/** * Returns {@code true} if there was an automatic removal due to eviction (the cause is neither * {@link RemovalCause#EXPLICIT} nor {@link RemovalCause#REPLACED}). * * @return if the entry was removed due to eviction */ public boolean wasEvicted() { return cause.wasEvicted(); }
@Override public void onRemoval(K key, V value, RemovalCause cause) { count.incrementAndGet(); lastNotification = RemovalNotification.create(key, value, com.google.common.cache.RemovalCause.valueOf(cause.name())); }
@DataProvider public Iterator<Object[]> notifications() { return Arrays.stream(RemovalCause.values()) .map(cause -> new Object[] { 1, new Expirable<>(2, 3), cause }) .iterator(); }
@Override public void onRemoval(RemovalNotification<K, V> notification) { RemovalCause cause = RemovalCause.valueOf(notification.getCause().name()); if (translateZeroExpire && (cause == RemovalCause.SIZE)) { // Guava internally uses sizing logic for null cache case cause = RemovalCause.EXPIRED; } delegate.onRemoval(notification.getKey(), notification.getValue(), cause); } }
/** * Constructor. */ TypeCache() { this.lruCache = Caffeine.newBuilder().maximumSize(8192).removalListener((RemovalListener<String, CachedType>) (key, value, cause) -> { if (RemovalCause.SIZE.equals(cause)) { softCache.put(key, value); } }).build(); this.softCache = Caffeine.newBuilder().softValues().build(); }
@Override public void onRemoval(Integer key, AtomicInteger value, RemovalCause cause) { if (cause.wasEvicted()) { evictionCount.incrementAndGet(); totalSum.addAndGet(value.get()); } } };
@Override public void onRemoval(K key, V value, RemovalCause cause) { add(RemovalNotification.create(key, value, com.google.common.cache.RemovalCause.valueOf(cause.name()))); } }
@Test(dataProvider = "notifications") public void publishIfEvicted(Integer key, Expirable<Integer> value, RemovalCause cause) { listener.delete(key, value, cause); if (cause.wasEvicted()) { if (cause == RemovalCause.EXPIRED) { verify(entryListener).onExpired(any()); } else { verify(entryListener).onRemoved(any()); } assertThat(statistics.getCacheEvictions(), is(1L)); } else { verify(entryListener, never()).onRemoved(any()); assertThat(statistics.getCacheEvictions(), is(0L)); } }
@Override public void delete(K key, @Nullable Expirable<V> expirable, RemovalCause cause) { if (cause.wasEvicted() && (expirable != null)) { V value = expirable.get(); if (cause == RemovalCause.EXPIRED) { dispatcher.publishExpiredQuietly(cache, key, value); } else { dispatcher.publishRemovedQuietly(cache, key, value); } statistics.recordEvictions(1L); } } }
@Override public void delete(@Nonnull Integer key, @Nullable ProfileBuilder value, @Nonnull RemovalCause cause) { if(cause.wasEvicted()) { // the expired profile was NOT flushed in time LOG.warn("Expired profile NOT flushed before removal, some state lost; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } else { // the expired profile was flushed successfully LOG.debug("Expired profile successfully flushed; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } } }
@Override public void delete(@Nonnull Integer key, @Nullable ProfileBuilder value, @Nonnull RemovalCause cause) { if(cause.wasEvicted()) { // add the profile to the expired cache expiredCache.put(key, value); LOG.debug("Profile expired from active cache due to inactivity; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } else { LOG.error("Profile removed from cache unexpectedly. File a bug report; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } } }
if (cause[0].wasEvicted()) { statsCounter().recordEviction(weight[0]);
private void visitorRemoved(String ipAddress, Optional<Visitor> optionalVisitor, RemovalCause cause) { if (cause.wasEvicted()) { if (optionalVisitor != null && optionalVisitor.isPresent()) repository.save(optionalVisitor.get()); } } }
if (cause[0].wasEvicted()) { statsCounter().recordEviction(weight[0]);
@Override public void delete(K key, @Nullable Expirable<V> expirable, RemovalCause cause) { if (cause.wasEvicted()) { @SuppressWarnings("NullAway") V value = expirable.get(); if (cause == RemovalCause.EXPIRED) { dispatcher.publishExpiredQuietly(cache, key, value); } else { dispatcher.publishRemovedQuietly(cache, key, value); } statistics.recordEvictions(1L); } } }