com.github.benmanes.caffeine.cache.RemovalCause Java Examples

The following examples show how to use com.github.benmanes.caffeine.cache.RemovalCause. 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: CaffeineProvider.java    From J2Cache with Apache License 2.0 6 votes vote down vote up
/**
 * 返回对 Caffeine cache 的 封装
 * @param region region name
 * @param size   max cache object size in memory
 * @param expire cache object expire time in millisecond
 *               if this parameter set to 0 or negative numbers
 *               means never expire
 * @param listener  j2cache cache listener
 * @return CaffeineCache
 */
private CaffeineCache newCaffeineCache(String region, long size, long expire, CacheExpiredListener listener) {
    Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
    caffeine = caffeine.maximumSize(size)
        .removalListener((k,v, cause) -> {
            /*
             * 程序删除的缓存不做通知处理,因为上层已经做了处理
             * 当缓存数据不是因为手工删除和超出容量限制而被删除的情况,就需要通知上层侦听器
             */
            if(cause != RemovalCause.EXPLICIT && cause != RemovalCause.REPLACED && cause != RemovalCause.SIZE)
                listener.notifyElementExpired(region, (String)k);
        });
    if (expire > 0) {
        caffeine = caffeine.expireAfterWrite(expire, TimeUnit.SECONDS);
    }
    com.github.benmanes.caffeine.cache.Cache<String, Object> loadingCache = caffeine.build();
    return new CaffeineCache(loadingCache, size, expire);
}
 
Example #2
Source File: TinyLfuBlockCache.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void onRemoval(BlockCacheKey key, Cacheable value, RemovalCause cause) {
  if (!cause.wasEvicted()) {
    // An explicit eviction (invalidation) is not added to the victim cache as the data may
    // no longer be valid for subsequent queries.
    return;
  }

  recordEviction();

  if (victimCache == null) {
    return;
  } else if (victimCache instanceof BucketCache) {
    BucketCache victimBucketCache = (BucketCache) victimCache;
    victimBucketCache.cacheBlockWithWait(key, value, /* inMemory */ true, /* wait */ true);
  } else {
    victimCache.cacheBlock(key, value);
  }
}
 
Example #3
Source File: CaffeinePolicy.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public CaffeinePolicy(Config config, Set<Characteristic> characteristics) {
  policyStats = new PolicyStats("product.Caffeine");
  BasicSettings settings = new BasicSettings(config);
  Caffeine<Long, AccessEvent> builder = Caffeine.newBuilder()
      .removalListener((Long key, AccessEvent value, RemovalCause cause) ->
          policyStats.recordEviction())
      .initialCapacity(settings.maximumSize())
      .executor(Runnable::run);
  if (characteristics.contains(WEIGHTED)) {
    builder.maximumWeight(settings.maximumSize());
    builder.weigher((key, value) -> value.weight());
  } else {
    builder.maximumSize(settings.maximumSize());
  }
  cache = builder.build();
}
 
Example #4
Source File: StatsCounterTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test
public void enabled() {
  ConcurrentStatsCounter counter = new ConcurrentStatsCounter();
  counter.recordHits(1);
  counter.recordMisses(1);
  counter.recordEviction();
  counter.recordEviction(10);
  counter.recordEviction(1, RemovalCause.SIZE);
  counter.recordLoadSuccess(1);
  counter.recordLoadFailure(1);
  CacheStats expected = new CacheStats(1, 1, 1, 1, 2, 3, 11);
  assertThat(counter.snapshot(), is(expected));
  assertThat(counter.toString(), is(expected.toString()));
  assertThat(counter.snapshot().toString(), is(expected.toString()));

  counter.incrementBy(counter);
  assertThat(counter.snapshot(), is(new CacheStats(2, 2, 2, 2, 4, 6, 22)));
}
 
Example #5
Source File: StatsCounterTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test
public void disabled() {
  StatsCounter counter = DisabledStatsCounter.INSTANCE;
  counter.recordHits(1);
  counter.recordMisses(1);
  counter.recordEviction();
  counter.recordEviction(10);
  counter.recordEviction(1, RemovalCause.SIZE);
  counter.recordLoadSuccess(1);
  counter.recordLoadFailure(1);
  assertThat(counter.snapshot(), is(new CacheStats(0, 0, 0, 0, 0, 0, 0)));
  assertThat(counter.toString(), is(new CacheStats(0, 0, 0, 0, 0, 0, 0).toString()));

  for (DisabledStatsCounter type : DisabledStatsCounter.values()) {
    assertThat(DisabledStatsCounter.valueOf(type.name()), is(counter));
  }
}
 
Example #6
Source File: JCacheEvictionListenerTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@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));
  }
}
 
Example #7
Source File: ExpiringAfterWriteCache.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
@Builder
public ExpiringAfterWriteCache(
    final long duration, final TimeUnit timeUnit, final BiConsumer<IdT, ValueT> removalListener) {
  cache =
      Caffeine.newBuilder()
          .expireAfterWrite(duration, timeUnit)
          .scheduler(Scheduler.systemScheduler())
          .removalListener(
              (IdT key, ValueT value, RemovalCause cause) -> {
                if (cause == RemovalCause.EXPIRED || cause == RemovalCause.EXPLICIT) {
                  removalListener.accept(key, value);
                }
              })
          .build();

  this.removalListener = removalListener;
}
 
Example #8
Source File: BlockCache.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void releaseLocation(BlockCacheKey blockCacheKey, BlockCacheLocation location, RemovalCause removalCause) {
  if (location == null) {
    return;
  }
  int bankId = location.getBankId();
  int block = location.getBlock();

  // mark the block removed before we release the lock to allow it to be reused
  location.setRemoved(true);

  locks[bankId].clear(block);
  lockCounters[bankId].decrementAndGet();
  for (OnRelease onRelease : onReleases) {
    onRelease.release(blockCacheKey);
  }
  if (removalCause.wasEvicted()) {
    metrics.blockCacheEviction.incrementAndGet();
  }
  metrics.blockCacheSize.decrementAndGet();
}
 
Example #9
Source File: AgentHostServiceImpl.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Override
public void onRemoval(@Nullable AgentHost agentHost,
                      @Nullable PoolManager<?> poolManager,
                      @Nonnull RemovalCause removalCause) {
    if (poolManager != null) {
        try {
            poolManager.close();
        } catch (Exception e) {
            log.warn("Unable to close agent host", e);
        }
    }

    if (agentHost != null) {
        log.info("Agent pool manager for host {} been closed", agentHost.getName());
    }
}
 
Example #10
Source File: AgentHostServiceImpl.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Override
public void onRemoval(@Nullable AgentHost agentHost,
                      @Nullable PoolManager<?> poolManager,
                      @Nonnull RemovalCause removalCause) {
    if (poolManager != null) {
        try {
            poolManager.close();
        } catch (Exception e) {
            log.warn("Unable to close agent host", e);
        }
    }

    if (agentHost != null) {
        log.info("Agent pool manager for host {} been closed", agentHost.getName());
    }
}
 
Example #11
Source File: IpStatRemovalListener.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(Object key, Object value, RemovalCause cause) {
	//		String ip = (String) key;
	IpStat ipStat = (IpStat) value;

	if (ipStatListener != null) {
		ipStatListener.onExpired(tioConfig, ipStat);
	}

}
 
Example #12
Source File: CaffeineCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
public CaffeineCache(int size, int expire, CacheExpiredListener listener) {
    cache = Caffeine.newBuilder()
            .maximumSize(size)
            .expireAfterAccess(expire, TimeUnit.SECONDS)
            .removalListener((k,v, cause) -> {
                //程序删除的缓存不做通知处理,因为上层已经做了处理
                if(cause != RemovalCause.EXPLICIT && cause != RemovalCause.REPLACED)
                    listener.notifyElementExpired((String)k);
            })
            .build();

    this.size = size;
    this.expire = expire;
}
 
Example #13
Source File: LoggingServiceImpl.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(String key, BufferedReader reader, RemovalCause cause) {
    if (Objects.isNull(reader)) {
        return;
    }

    try {
        reader.close();
    } catch (IOException e) {
        log.debug(e);
    }
}
 
Example #14
Source File: RemovalListeners.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(K key, V value, RemovalCause cause) {
  validate(key, value, cause);

  if (reject) {
    rejected++;
    throw new RejectedExecutionException("Rejected eviction of " +
        new RemovalNotification<>(key, value, cause));
  }
}
 
Example #15
Source File: RemovalListeners.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private static void validate(Object key, Object value, RemovalCause cause) {
  if (cause != RemovalCause.COLLECTED) {
    requireNonNull(key);
    requireNonNull(value);
  }
  requireNonNull(cause);
}
 
Example #16
Source File: GuavaCacheFromContext.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: StatsCounterTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void guarded_exception() {
  StatsCounter statsCounter = Mockito.mock(StatsCounter.class);
  when(statsCounter.snapshot()).thenThrow(new NullPointerException());
  doThrow(NullPointerException.class).when(statsCounter).recordEviction();
  doThrow(NullPointerException.class).when(statsCounter).recordHits(anyInt());
  doThrow(NullPointerException.class).when(statsCounter).recordMisses(anyInt());
  doThrow(NullPointerException.class).when(statsCounter).recordEviction(anyInt(), any());
  doThrow(NullPointerException.class).when(statsCounter).recordLoadSuccess(anyLong());
  doThrow(NullPointerException.class).when(statsCounter).recordLoadFailure(anyLong());

  StatsCounter guarded = StatsCounter.guardedStatsCounter(statsCounter);
  guarded.recordHits(1);
  guarded.recordMisses(1);
  guarded.recordEviction();
  guarded.recordEviction(10);
  guarded.recordEviction(1, RemovalCause.SIZE);
  guarded.recordLoadSuccess(1);
  guarded.recordLoadFailure(1);
  assertThat(guarded.snapshot(), is(CacheStats.empty()));

  verify(statsCounter).recordHits(1);
  verify(statsCounter).recordMisses(1);
  verify(statsCounter).recordEviction();
  verify(statsCounter).recordEviction(10);
  verify(statsCounter).recordEviction(1, RemovalCause.SIZE);
  verify(statsCounter).recordLoadSuccess(1);
  verify(statsCounter).recordLoadFailure(1);
}
 
Example #18
Source File: StatsCounterTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void guarded() {
  StatsCounter counter = StatsCounter.guardedStatsCounter(new ConcurrentStatsCounter());
  counter.recordHits(1);
  counter.recordMisses(1);
  counter.recordEviction();
  counter.recordEviction(10);
  counter.recordEviction(1, RemovalCause.SIZE);
  counter.recordLoadSuccess(1);
  counter.recordLoadFailure(1);
  CacheStats expected = new CacheStats(1, 1, 1, 1, 2, 3, 11);
  assertThat(counter.snapshot(), is(expected));
  assertThat(counter.toString(), is(expected.toString()));
  assertThat(counter.snapshot().toString(), is(expected.toString()));
}
 
Example #19
Source File: SearchServiceImpl.java    From inception with Apache License 2.0 5 votes vote down vote up
/**
 * Unloads the index state and deactivates/closes the underlying physical index.
 */
private void unloadIndex(Long aProjectId, Index aIndex, RemovalCause aCause)
{
    if (aIndex.getPhysicalIndex() != null && aIndex.getPhysicalIndex().isOpen()) {
        log.trace("Unloading index for project [{}]({})", aIndex.getProject().getName(),
                aIndex.getProject().getId());
        aIndex.getPhysicalIndex().close();
    }
}
 
Example #20
Source File: Issue412Test.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void expire_remove() {
  timeTasks(NUM_THREADS, this::addRemoveAndExpire);
  shutdownAndAwaitTermination(executor, 1, TimeUnit.MINUTES);

  Multiset<RemovalCause> causes = listener.evicted().stream()
      .map(RemovalNotification::getCause)
      .collect(toImmutableMultiset());
  assertThat(causes, not(hasItem(RemovalCause.COLLECTED)));
}
 
Example #21
Source File: GuardedStatsCounter.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Override
public void recordEviction(int weight, RemovalCause cause) {
  try {
    delegate.recordEviction(weight, cause);
  } catch (Throwable t) {
    logger.log(Level.WARNING, "Exception thrown by stats counter", t);
  }
}
 
Example #22
Source File: JCacheEvictionListener.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #23
Source File: LoggingServiceImpl.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(String key, BufferedReader reader, RemovalCause cause) {
    if (Objects.isNull(reader)) {
        return;
    }

    try {
        reader.close();
    } catch (IOException e) {
        log.debug(e);
    }
}
 
Example #24
Source File: CaffeineCache.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(K key, V value, RemovalCause cause) {
  ramBytes.add(
      - (RamUsageEstimator.sizeOfObject(key, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED) +
      RamUsageEstimator.sizeOfObject(value, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED) +
      RamUsageEstimator.LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY)
  );
}
 
Example #25
Source File: CaffeineSpanCache.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * Zipkin trace finished by the expired rule.
 */
@Override
public void onRemoval(@Nullable String key, @Nullable ZipkinTrace trace, @Nonnull RemovalCause cause) {
    if (trace instanceof ZipkinTrace.TriggerTrace) {
        return;
    }
    try {
        Zipkin2SkyWalkingTransfer.INSTANCE.transfer(trace);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        logger.warn("Zipkin trace:" + trace);
    }
}
 
Example #26
Source File: AbstractCacheTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testSingleSimpleStats() {
  StatsCounter counter = new ConcurrentStatsCounter();
  for (int i = 0; i < 11; i++) {
    counter.recordHits(1);
  }
  for (int i = 0; i < 13; i++) {
    counter.recordLoadSuccess(i);
  }
  for (int i = 0; i < 17; i++) {
    counter.recordLoadFailure(i);
  }
  for (int i = 0; i < 23; i++) {
    counter.recordMisses(1);
  }
  for (int i = 0; i < 27; i++) {
    counter.recordEviction(1, RemovalCause.SIZE);
  }
  CacheStats stats = counter.snapshot();
  int requestCount = 11 + 23;
  assertEquals(requestCount, stats.requestCount());
  assertEquals(11, stats.hitCount());
  assertEquals(11.0 / requestCount, stats.hitRate(), 0.0);
  int missCount = 23;
  assertEquals(missCount, stats.missCount());
  assertEquals(((double) missCount) / requestCount, stats.missRate(), 0.0);
  assertEquals(13, stats.loadSuccessCount());
  assertEquals(17, stats.loadFailureCount());
  assertEquals(13 + 17, stats.loadCount());
  assertEquals(214, stats.totalLoadTime());
  assertEquals(214.0 / (13 + 17), stats.averageLoadPenalty(), 0.0);
  assertEquals(27, stats.evictionCount());
}
 
Example #27
Source File: TokenPathAccessStatRemovalListener.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public void onRemoval(Object key, Object value, RemovalCause cause) {
	String token = (String) key;
	TokenAccessStat tokenAccessStat = (TokenAccessStat) value;

	if (tokenPathAccessStatListener != null) {
		tokenPathAccessStatListener.onExpired(tioConfig, token, tokenAccessStat);
	}

}
 
Example #28
Source File: TestCaffeineCache.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxIdleTime() throws Exception {
  int IDLE_TIME_SEC = 5;
  CountDownLatch removed = new CountDownLatch(1);
  AtomicReference<RemovalCause> removalCause = new AtomicReference<>();
  CaffeineCache<String, String> cache = new CaffeineCache<>() {
    @Override
    public void onRemoval(String key, String value, RemovalCause cause) {
      super.onRemoval(key, value, cause);
      removalCause.set(cause);
      removed.countDown();
    }
  };
  Map<String, String> params = new HashMap<>();
  params.put("size", "6");
  params.put("maxIdleTime", "" + IDLE_TIME_SEC);
  cache.init(params, null, new NoOpRegenerator());

  cache.put("foo", "bar");
  assertEquals("bar", cache.get("foo"));
  // sleep for at least the idle time before inserting other entries
  // the eviction is piggy-backed on put()
  Thread.sleep(TimeUnit.SECONDS.toMillis(IDLE_TIME_SEC * 2));
  cache.put("abc", "xyz");
  boolean await = removed.await(30, TimeUnit.SECONDS);
  assertTrue("did not expire entry in in time", await);
  assertEquals(RemovalCause.EXPIRED, removalCause.get());
  assertNull(cache.get("foo"));
}
 
Example #29
Source File: DefaultMessageDistributor.java    From metron with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #30
Source File: DefaultMessageDistributor.java    From metron with Apache License 2.0 5 votes vote down vote up
@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);
  }
}