下面列出了com.google.common.cache.RemovalNotification#getKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onRemoval(RemovalNotification<Integer, StatementInfo> notification) {
Integer stmtId = notification.getKey();
StatementInfo doomed = notification.getValue();
if (doomed == null) {
// log/throw?
return;
}
LOGGER.debug("Expiring statement {} because {}", stmtId, notification.getCause());
try {
if (doomed.getResultSet() != null) {
doomed.getResultSet().close();
}
if (doomed.statement != null) {
doomed.statement.close();
}
} catch (Throwable t) {
LOGGER.info("Exception thrown while expiring statement {}", stmtId, t);
}
}
@Override
public void onRemoval(RemovalNotification<DataSegmentFragment, FragmentData> notification) {
DataSegmentFragment fragment = notification.getKey();
logger.debug("Data fragment " + fragment + " is unloaded from Cache due to "
+ notification.getCause());
FragmentData fragmentData = notification.getValue();
AtomicLong refCounter = refCounters.get(fragment);
if (refCounter != null) {
synchronized (refCounter) {
if (refCounter.get() <= 0) {
int bufferSize = fragmentData.getBufferCapacity();
currentBufferedSize.addAndGet(-bufferSize);
fragmentData.tryForceUnMapBuffer();
refCounters.remove(fragment);
} else {
logger.debug("Fragment mapped buffer " + fragment
+ " cannot be cleaned, because it has reference " + refCounter.get());
}
}
} else {
logger.debug("no ref counter found for fragment: " + fragment);
}
}
@Override
public void onRemoval(RemovalNotification<Object, Cache<Query, Value>> notification) {
Object key = notification.getKey();
if (key == null) {
return;
}
Cache<Query, Value> valueCache = notification.getValue();
if (valueCache == null) {
return;
}
for (Value value : valueCache.asMap().values()) {
listener.onRemoval(value.shardId, value.bitset);
// if null then this means the shard has already been removed and the stats are 0 anyway for the shard this key belongs to
}
}
public void onRemoval(RemovalNotification<Integer, StatementInfo> notification) {
Integer stmtId = notification.getKey();
StatementInfo doomed = notification.getValue();
if (doomed == null) {
// log/throw?
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Expiring statement " + stmtId + " because "
+ notification.getCause());
}
try {
if (doomed.resultSet != null) {
doomed.resultSet.close();
}
if (doomed.statement != null) {
doomed.statement.close();
}
} catch (Throwable t) {
LOG.info("Exception thrown while expiring statement " + stmtId);
}
}
/**
* Full constructor.
*
* @param duration
* How long inactive things should be kept in the cache.
* @param unit
* The <code>duration</code>'s unit of measurement.
* @param expiryHandler
* Callback that is given all expired values from the cache just
* before they are thrown away.
*/
UsageTrackingCache(final long duration, @Nonnull final TimeUnit unit,
@Nonnull final ExpiryHandler<K, V> expiryHandler) {
activeCacheByKey = new HashMap<>();
activeCacheByValue = new IdentityHashMap();
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
cacheBuilder = cacheBuilder.expireAfterAccess(duration, unit);
final RemovalListener removalHandler = new RemovalListener<K, CacheEntry<K, V>>() {
@Override
public void onRemoval(RemovalNotification<K, CacheEntry<K, V>> notification) {
final K key = notification.getKey();
if (!activeCacheByKey.containsKey(key)) {
final CacheEntry<K, V> record = notification.getValue();
final V value = record.getValue();
expiryHandler.entryDroppedFromCache(key, value);
}
}
};
cacheBuilder = cacheBuilder.removalListener(removalHandler);
durationCache = cacheBuilder.build();
}
private void close(RemovalNotification<String, OutputStream> notification) {
String filePath = notification.getKey();
OutputStream outputStream = notification.getValue();
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
logger.error("close file from agent error. path: {}", filePath, e);
}
}
public void onRemoval(RemovalNotification<String, Connection> notification) {
String connectionId = notification.getKey();
Connection doomed = notification.getValue();
LOGGER.debug("Expiring connection {} because {}", connectionId, notification.getCause());
try {
if (doomed != null) {
doomed.close();
}
} catch (Throwable t) {
LOGGER.info("Exception thrown while expiring connection {}", connectionId, t);
}
}
@Override
public void onRemoval(RemovalNotification<Key, Value> notification) {
if (notification.getKey() == null) {
return;
}
notification.getKey().shard.requestCache().onRemoval(notification);
}
@Override
public void onRemoval(RemovalNotification<IndicesRequestCache.Key, IndicesRequestCache.Value> removalNotification) {
if (removalNotification.wasEvicted()) {
evictionsMetric.inc();
}
long dec = 0;
if (removalNotification.getKey() != null) {
dec += removalNotification.getKey().ramBytesUsed();
}
if (removalNotification.getValue() != null) {
dec += removalNotification.getValue().ramBytesUsed();
}
totalMetric.dec(dec);
}
public void onRemoval(RemovalNotification<String, Connection> notification) {
String connectionId = notification.getKey();
Connection doomed = notification.getValue();
LOG.debug("Expiring connection {} because {}", connectionId, notification.getCause());
try {
if (doomed != null) {
doomed.close();
}
} catch (Throwable t) {
LOG.info("Exception thrown while expiring connection {}", connectionId, t);
}
}
/**
* Atomically transitions the resource to a state where it can no longer be used. If the
* resource is idle or retired then it is immediately discarded. If the resource is
* currently in use then it is marked to be discarded when it has been released.
*/
@Override
public void onRemoval(RemovalNotification<ResourceKey<K>, R> notification) {
ResourceKey<K> resourceKey = notification.getKey();
for (;;) {
Status status = resourceKey.getStatus();
switch (status) {
case IDLE:
// The resource is not being used and may be immediately discarded
if (resourceKey.goFromIdleToDead()) {
discardFromIdle(resourceKey, notification.getValue());
return;
}
break;
case IN_FLIGHT:
// The resource is currently being used and should be discarded when released
if (resourceKey.goFromInFlightToRetired()) {
return;
}
break;
case RETIRED:
// A resource is already retired when it has been expired by the idle cache
if (resourceKey.goFromRetiredToDead()) {
discardFromRetired(resourceKey, notification.getValue());
return;
}
break;
default:
throw new IllegalStateException("Unnexpected state: " + status);
}
}
}
@Override
public void onRemoval(final RemovalNotification<String, List<MemberDescriptor>> notification) {
final RemovalCause cause = notification.getCause();
if (cause.equals(RemovalCause.EXPLICIT)) {
final String key = notification.getKey();
boolean b = ProjectDatabaseHelper.deleteMemberDescriptors(key);
}
}
private void removalListener(RemovalNotification<UUID, SessionEntry> notification)
{
UUID uuid = notification.getKey();
Instant now = Instant.now();
try (Connection con = sql2o.open())
{
con.createQuery("update sessions set last_used = :last_used where uuid = :uuid")
.addParameter("last_used", Timestamp.from(now))
.addParameter("uuid", uuid.toString())
.executeUpdate();
}
}
/**
* Creates the removal listener which is attached to the cache.
*
* @return cache entry removal listener.
*/
private RemovalListener<String, FSFilterStreamContext> createCacheRemoveListener()
{
//When an entry is removed from the cache, removal listener is notified and it closes the output stream.
return new RemovalListener<String, FSFilterStreamContext>()
{
@Override
public void onRemoval(@Nonnull RemovalNotification<String, FSFilterStreamContext> notification)
{
FSFilterStreamContext streamContext = notification.getValue();
if (streamContext != null) {
try {
String filename = notification.getKey();
String partFileName = getPartFileNamePri(filename);
LOG.info("closing {}", partFileName);
long start = System.currentTimeMillis();
closeStream(streamContext);
filesWithOpenStreams.remove(filename);
totalWritingTime += System.currentTimeMillis() - start;
} catch (IOException e) {
LOG.error("removing {}", notification.getValue(), e);
throw new RuntimeException(e);
}
}
}
};
}
public void onRemoval(RemovalNotification<String, Connection> notification) {
String connectionId = notification.getKey();
Connection doomed = notification.getValue();
if (LOG.isDebugEnabled()) {
LOG.debug("Expiring connection " + connectionId + " because "
+ notification.getCause());
}
try {
if (doomed != null) {
doomed.close();
}
} catch (Throwable t) {
LOG.info("Exception thrown while expiring connection " + connectionId, t);
}
}
@Override
public void onRemoval(RemovalNotification<ByteBuf, String> notification) {
ByteBuf key = notification.getKey();
if (key == null) {
throw new IllegalStateException("Unexpected null key");
}
boolean release = key.release();
if (!release) {
LOGGER.warn("The cached string ByteBuf with value {} was "
+ "removed but it had more references than expected", notification.getValue());
}
}
@Override
public void onRemoval(RemovalNotification<IpmiReceiverKey, IpmiReceiver> notification) {
IpmiReceiverKey key = notification.getKey();
IpmiReceiver receiver = notification.getValue();
if (key != null && receiver != null && notification.wasEvicted())
receiver.timeout(key);
}
@Override
public void onRemoval(RemovalNotification<GuidePostsKey, GuidePostsInfo> notification) {
if (logger.isTraceEnabled()) {
final RemovalCause cause = notification.getCause();
if (wasEvicted(cause)) {
GuidePostsKey key = notification.getKey();
logger.trace("Cached stats for {} with size={}bytes was evicted due to cause={}",
new Object[] {key, notification.getValue().getEstimatedSize(),
cause});
}
}
}
@Override
public void onRemoval(RemovalNotification<CacheKey, V> notification) {
if(notification.getKey() != null && notification.wasEvicted()) {
segmentIndex.remove(notification.getKey().segmentKey,notification.getKey());
}
EvictionListener<V> evictionListener = evictionListeners.get(notification.getKey().segmentKey);
// only notify when it was not evicted explicitly (when a entry was deleted)
// otherwise the prePassivate will run
if(evictionListener != null && notification.wasEvicted()) {
evictionListener.onEvicted(notification.getValue());
}
}
public void onRemoval(RemovalNotification<String, NotifyHandler> notification) {
String description;
switch (notification.getCause()) {
case REPLACED:
description = "出现重复的ssid的notifyHandler,原ssid对应的notifyHandler被移除,ssid=" + notification.getKey();
LOG.error(new JSONObject() {{
put("type", "notifyHandlerMapRemoval");
put("mapSize", handleMap.size());
put("cause", notification.getCause().name());
put("ssid", notification.getKey());
put("notifyHandler", notification.getValue());
put("description", description);
}});
break;
case EXPIRED:
description = "notifyHandler已过期:" + notification.getKey();
LOG.info(new JSONObject() {{
put("type", "notifyHandlerMapRemoval");
put("mapSize", handleMap.size());
put("cause", notification.getCause().name());
put("ssid", notification.getKey());
put("notifyHandler", notification.getValue());
put("description", description);
}});
break;
case SIZE:
description = "notifyHandlerMap的size超过上限,可能是内存泄漏";
LOG.info(new JSONObject() {{
put("type", "notifyHandlerMapRemoval");
put("mapSize", handleMap.size());
put("cause", notification.getCause().name());
put("ssid", notification.getKey());
put("notifyHandler", notification.getValue());
put("description", description);
}});
break;
default:
LOG.debug(new JSONObject() {{
put("type", "notifyHandlerMapRemoval");
put("mapSize", handleMap.size());
put("cause", notification.getCause().name());
put("ssid", notification.getKey());
put("notifyHandler", notification.getValue());
put("description", "正常删除");
}});
}
}