Java Code Examples for com.google.common.cache.RemovalNotification#wasEvicted()

The following examples show how to use com.google.common.cache.RemovalNotification#wasEvicted() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuavaCacheUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@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());
}
 
Example 2
Source File: ShardRequestCache.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: LogSegmentMetadataCache.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(RemovalNotification<String, LogSegmentMetadata> notification) {
    if (notification.wasEvicted()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Log segment of {} was evicted.", notification.getKey());
        }
    }
}
 
Example 4
Source File: NettyPistachioClientHandler.java    From Pistachio with Apache License 2.0 5 votes vote down vote up
@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"));
    }
}
 
Example 5
Source File: IpmiPayloadReceiveDispatcher.java    From ipmi4j with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: SimpleVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@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()));
    }
}
 
Example 7
Source File: SimpleFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@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()));
    }
}
 
Example 8
Source File: CacheManager.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 9
Source File: StatusUpdaterBolt.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: IndexerBolt.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
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());
}