下面列出了com.google.common.cache.RemovalNotification#wasEvicted ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void whenEntryRemovedFromCache_thenNotify() {
final CacheLoader<String, String> loader = new CacheLoader<String, String>() {
@Override
public final String load(final String key) {
return key.toUpperCase();
}
};
final RemovalListener<String, String> listener = new RemovalListener<String, String>() {
@Override
public void onRemoval(final RemovalNotification<String, String> n) {
if (n.wasEvicted()) {
final String cause = n.getCause().name();
assertEquals(RemovalCause.SIZE.toString(), cause);
}
}
};
final LoadingCache<String, String> cache = CacheBuilder.newBuilder().maximumSize(3).removalListener(listener).build(loader);
cache.getUnchecked("first");
cache.getUnchecked("second");
cache.getUnchecked("third");
cache.getUnchecked("last");
assertEquals(3, cache.size());
}
@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);
}
@Override
public void onRemoval(RemovalNotification<String, LogSegmentMetadata> notification) {
if (notification.wasEvicted()) {
if (logger.isDebugEnabled()) {
logger.debug("Log segment of {} was evicted.", notification.getKey());
}
}
}
@Override
public void onRemoval(
RemovalNotification<Integer, SettableFuture<Response>> arg0) {
if (arg0.wasEvicted()) {
SettableFuture<Response> response = arg0.getValue();
logger.warn("request id {} timeout", arg0.getKey());
response.setException(new RequestTimeoutException("request timeout"));
}
}
@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<Long,
SettableFuture<CompletedBatchOperation>> notification) {
// wrapping in ExecutionException to support Future.get
if (notification.wasEvicted()) {
notification.getValue()
.setException(new ExecutionException("Timed out",
new TimeoutException()));
}
}
@Override
public void onRemoval(RemovalNotification<Integer, SettableFuture<CompletedBatchOperation>> notification) {
// wrapping in ExecutionException to support Future.get
if (notification.wasEvicted()) {
notification.getValue()
.setException(new ExecutionException("Timed out",
new TimeoutException()));
}
}
@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, List<Tuple>> removal) {
if (!removal.wasEvicted())
return;
LOG.error("Purged from waitAck {} with {} values", removal.getKey(),
removal.getValue().size());
for (Tuple t : removal.getValue()) {
_collector.fail(t);
}
}
public void onRemoval(RemovalNotification<String, Tuple> removal) {
if (!removal.wasEvicted())
return;
LOG.error("Purged from waitAck {} - {}", removal.getKey(), removal
.getValue().getStringByField("url"));
_collector.fail(removal.getValue());
}