Refine search
public void testGet_expireAfterAccess() { Object computed = new Object(); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .expireAfterAccess(0, SECONDS) .removalListener(listener) .build(constantLoader(computed)); Object key = new Object(); assertSame(computed, cache.getUnchecked(key)); RemovalNotification<Object, Object> notification = listener.remove(); assertSame(key, notification.getKey()); assertSame(computed, notification.getValue()); assertSame(RemovalCause.SIZE, notification.getCause()); assertTrue(listener.isEmpty()); checkEmpty(cache); }
@Override public void onRemoval(RemovalNotification<Integer, AtomicInteger> notification) { if (notification.wasEvicted()) { evictionCount.incrementAndGet(); totalSum.addAndGet(notification.getValue().get()); } } };
@Override public void onRemoval(RemovalNotification<Long, InternalVertex> notification) { if (notification.getCause() == RemovalCause.EXPLICIT) { //Due to invalidation at the end assert volatileVertices.isEmpty(); return; } //Should only get evicted based on size constraint or replaced through add assert (notification.getCause() == RemovalCause.SIZE || notification.getCause() == RemovalCause.REPLACED) : "Cause: " + notification.getCause(); InternalVertex v = notification.getValue(); if (v.isModified()) { volatileVertices.putIfAbsent(notification.getKey(), v); } } })
public GuavaVertexCache(final long maxCacheSize, final int concurrencyLevel, final int initialDirtySize) { volatileVertices = new NonBlockingHashMapLong<>(initialDirtySize); log.debug("Created dirty vertex map with initial size {}", initialDirtySize); cache = CacheBuilder.newBuilder().maximumSize(maxCacheSize).concurrencyLevel(concurrencyLevel) .removalListener((RemovalListener<Long, InternalVertex>) notification -> { if (notification.getCause() == RemovalCause.EXPLICIT) { //Due to invalidation at the end assert volatileVertices.isEmpty(); return; } //Should only get evicted based on size constraint or replaced through add assert (notification.getCause() == RemovalCause.SIZE || notification.getCause() == RemovalCause.REPLACED) : "Cause: " + notification.getCause(); final InternalVertex v = notification.getValue(); if (((AbstractVertex) v).isTxOpen() && v.isModified()) { volatileVertices.putIfAbsent(notification.getKey(), v); } }) .build(); log.debug("Created vertex cache with max size {}", maxCacheSize); }
@Inject public NetflowV9CodecAggregator() { // TODO customize this.templateCache = CacheBuilder.newBuilder() .maximumSize(5000) .removalListener(notification -> LOG.debug("Removed {} from template cache for reason {}", notification.getKey(), notification.getCause())) .recordStats() .build(); this.packetCache = CacheBuilder.newBuilder() .expireAfterWrite(1, TimeUnit.MINUTES) .maximumWeight(Size.megabytes(1).toBytes()) .removalListener((RemovalListener<TemplateKey, Queue<PacketBytes>>) notification -> LOG.debug("Removed {} from packet cache for reason {}", notification.getKey(), notification.getCause())) .weigher((key, value) -> value.stream().map(PacketBytes::readableBytes).reduce(0, Integer::sum)) .recordStats() .build(); }
public void testGet_expireAfterWrite() { Object computed = new Object(); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .expireAfterWrite(0, SECONDS) .removalListener(listener) .build(constantLoader(computed)); Object key = new Object(); assertSame(computed, cache.getUnchecked(key)); RemovalNotification<Object, Object> notification = listener.remove(); assertSame(key, notification.getKey()); assertSame(computed, notification.getValue()); assertSame(RemovalCause.SIZE, notification.getCause()); assertTrue(listener.isEmpty()); checkEmpty(cache); }
CacheBuilder.newBuilder() .recordStats() .concurrencyLevel(2) .expireAfterWrite(100, MILLISECONDS) .removalListener(removalListener) assertEquals("Invalid removal notification", notification.getKey(), notification.getValue());
final WatchKeysUtil watchKeysUtil, final GrayReleaseRulesHolder grayReleaseRulesHolder) { localCache = CacheBuilder.newBuilder() .expireAfterWrite(EXPIRE_AFTER_WRITE, TimeUnit.MINUTES) .weigher((Weigher<String, String>) (key, value) -> value == null ? 0 : value.length()) .maximumWeight(MAX_CACHE_SIZE) .removalListener(notification -> { String cacheKey = notification.getKey(); logger.debug("removing cache key: {}", cacheKey); if (!cacheKey2WatchedKeys.containsKey(cacheKey)) {
QueuingRemovalListener<String, String> listener = queuingRemovalListener(); final LoadingCache<String, String> cache = CacheBuilder.newBuilder() .removalListener(listener) .concurrencyLevel(20) .build(new DelayingIdentityLoader<String>(computationShouldWait, computationLatch)); removalNotifications.put(notification.getKey(), notification.getValue()); assertEquals( "Unexpected key/value pair passed to removalListener", notification.getKey(), notification.getValue());
this.verboseLogging = graph.getConfiguration().getConfiguration() .get(GraphDatabaseConfiguration.VERBOSE_TX_RECOVERY); this.txCache = CacheBuilder.newBuilder() .concurrencyLevel(2) .initialCapacity(100) .expireAfterWrite(maxTxLength.toNanos(), TimeUnit.NANOSECONDS) .removalListener((RemovalListener<StandardTransactionId, TxEntry>) notification -> { final RemovalCause cause = notification.getCause(); Preconditions.checkArgument(cause == RemovalCause.EXPIRED, "Unexpected removal cause [%s] for transaction [%s]", cause, notification.getKey()); final TxEntry entry = notification.getValue(); if (entry.status == LogTxStatus.SECONDARY_FAILURE || entry.status == LogTxStatus.PRIMARY_SUCCESS) { failureTxCounter.incrementAndGet(); fixSecondaryFailure(notification.getKey(), entry); } else { successTxCounter.incrementAndGet();
public void testExpiration_invalidateAll() { FakeTicker ticker = new FakeTicker(); QueuingRemovalListener<Integer, Integer> listener = TestingRemovalListeners.queuingRemovalListener(); Cache<Integer, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(1, TimeUnit.MINUTES) .removalListener(listener) .ticker(ticker) .build(); cache.put(1, 1); ticker.advance(10, TimeUnit.MINUTES); cache.invalidateAll(); assertThat(listener.poll().getCause()).isEqualTo(RemovalCause.EXPIRED); }
/** * Create a new cache * @param maximumSize the cache's maximum size in bytes * @param maximumTime the maximum number of seconds a chunk stays in the cache */ IndexableChunkCache(long maximumSize, long maximumTime) { this.maximumSize = maximumSize; cache = CacheBuilder.newBuilder() .expireAfterWrite(maximumTime, TimeUnit.SECONDS) .<String, Buffer>removalListener(n -> size.addAndGet(-n.getValue().length())) .build(); }
public IdGenerator(List<Storage> storages, TimeWatcher timeWatcher) { localIdGenerator = new LocalIdGenerator(); this.timeWatcher = timeWatcher; this.storages = storages; sequenceLocks = CacheBuilder.newBuilder().build(); sequenceCache = CacheBuilder.<String, CacheSequence>newBuilder().expireAfterWrite( EXPIRE_DURATION, TimeUnit.SECONDS).removalListener(notification -> { String key = (String) notification.getKey(); if (null != key) sequenceLocks.invalidate(key); }).build(); }
@Override public void onRemoval(RemovalNotification<String, File> arg0) { if(arg0.getValue().delete()) { if(arg0.wasEvicted()) { log.info("Deleted recording due to excess videos: " + arg0.getKey()); } } } }).build();
public V getLastEvictedValue() { return lastNotification.getValue(); }
public void testSegmentStoreComputedValue() { QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).removalListener(listener)); Segment<Object, Object> segment = map.segments[0]; RemovalNotification<Object, Object> notification = listener.remove(); assertEquals(immutableEntry(key, value2), notification); assertEquals(RemovalCause.REPLACED, notification.getCause()); assertTrue(listener.isEmpty()); notification = listener.remove(); assertEquals(immutableEntry(key, value3), notification); assertEquals(RemovalCause.REPLACED, notification.getCause()); assertTrue(listener.isEmpty()); notification = listener.remove(); assertEquals(immutableEntry(key, null), notification); assertEquals(RemovalCause.COLLECTED, notification.getCause()); assertTrue(listener.isEmpty());
private void stateRemoved(final RemovalNotification<Identifier, AssembledMessageState> notification) { if (notification.wasEvicted()) { LOG.warn("{}: AssembledMessageState for {} was expired from the cache", logContext, notification.getKey()); } else { LOG.debug("{}: AssembledMessageState for {} was removed from the cache due to {}", logContext, notification.getKey(), notification.getCause()); } notification.getValue().close(); }