com.github.benmanes.caffeine.cache.RemovalListener Java Examples
The following examples show how to use
com.github.benmanes.caffeine.cache.RemovalListener.
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: CaffeineCache.java From t-io with Apache License 2.0 | 6 votes |
public static CaffeineCache register(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds, RemovalListener<String, Serializable> removalListener) { CaffeineCache caffeineCache = map.get(cacheName); if (caffeineCache == null) { synchronized (CaffeineCache.class) { caffeineCache = map.get(cacheName); if (caffeineCache == null) { Integer initialCapacity = 10; Integer maximumSize = 5000000; boolean recordStats = false; LoadingCache<String, Serializable> loadingCache = CaffeineUtils.createLoadingCache(cacheName, timeToLiveSeconds, timeToIdleSeconds, initialCapacity, maximumSize, recordStats, removalListener); Integer temporaryMaximumSize = 500000; LoadingCache<String, Serializable> temporaryLoadingCache = CaffeineUtils.createLoadingCache(cacheName, 10L, (Long) null, initialCapacity, temporaryMaximumSize, recordStats, removalListener); caffeineCache = new CaffeineCache(cacheName, loadingCache, temporaryLoadingCache); caffeineCache.setTimeToIdleSeconds(timeToIdleSeconds); caffeineCache.setTimeToLiveSeconds(timeToLiveSeconds); map.put(cacheName, caffeineCache); } } } return caffeineCache; }
Example #2
Source File: CacheHelper.java From flow-platform-x with Apache License 2.0 | 5 votes |
public static <K, V> Cache<K, V> createLocalCache(int maxSize, int expireInSeconds, RemovalListener<K, V> listener) { return Caffeine.newBuilder() .initialCapacity(maxSize) .maximumSize(maxSize) .removalListener(listener) .expireAfterWrite(expireInSeconds, TimeUnit.SECONDS) .build(); }
Example #3
Source File: LoginAttemptService.java From zhcet-web with Apache License 2.0 | 5 votes |
@Autowired public LoginAttemptService(ConfigurationService configurationService) { this.configurationService = configurationService; RemovalListener<String, Integer> removalListener = (key, value, cause) -> log.debug("Login Key {} with value {} was removed because : {}", key, value, cause); attemptsCache = Caffeine .newBuilder() .maximumSize(10000) .removalListener(removalListener) .expireAfterWrite(Duration.of(getBlockDuration(), TIME_UNIT)) .build(key -> 0); }
Example #4
Source File: CacheHelper.java From flow-platform-x with Apache License 2.0 | 5 votes |
public static <K, V> Cache<K, V> createLocalCache(int maxSize, int expireInSeconds, RemovalListener<K, V> listener) { return Caffeine.newBuilder() .initialCapacity(maxSize) .maximumSize(maxSize) .removalListener(listener) .expireAfterWrite(expireInSeconds, TimeUnit.SECONDS) .build(); }
Example #5
Source File: BlockCache.java From lucene-solr with Apache License 2.0 | 5 votes |
public BlockCache(Metrics metrics, boolean directAllocation, long totalMemory, int slabSize, int blockSize) { this.metrics = metrics; numberOfBlocksPerBank = slabSize / blockSize; int numberOfBanks = (int) (totalMemory / slabSize); banks = new ByteBuffer[numberOfBanks]; locks = new BlockLocks[numberOfBanks]; lockCounters = new AtomicInteger[numberOfBanks]; maxEntries = (numberOfBlocksPerBank * numberOfBanks) - 1; for (int i = 0; i < numberOfBanks; i++) { if (directAllocation) { banks[i] = ByteBuffer.allocateDirect(numberOfBlocksPerBank * blockSize); } else { banks[i] = ByteBuffer.allocate(numberOfBlocksPerBank * blockSize); } locks[i] = new BlockLocks(numberOfBlocksPerBank); lockCounters[i] = new AtomicInteger(); } RemovalListener<BlockCacheKey,BlockCacheLocation> listener = (blockCacheKey, blockCacheLocation, removalCause) -> releaseLocation(blockCacheKey, blockCacheLocation, removalCause); cache = Caffeine.newBuilder() .removalListener(listener) .maximumSize(maxEntries) .build(); this.blockSize = blockSize; }
Example #6
Source File: FileService.java From armeria with Apache License 2.0 | 5 votes |
private static Cache<PathAndEncoding, AggregatedHttpFile> newCache(String cacheSpec) { final Caffeine<Object, Object> b = Caffeine.from(cacheSpec); b.recordStats() .removalListener((RemovalListener<PathAndEncoding, AggregatedHttpFile>) (key, value, cause) -> { if (value != null) { final HttpData content = value.content(); if (content instanceof ByteBufHolder) { ((ByteBufHolder) content).release(); } } }); return b.build(); }
Example #7
Source File: CacheBuilderTest.java From caffeine with Apache License 2.0 | 5 votes |
public void testRemovalListener_setTwice() { RemovalListener<Object, Object> testListener = nullRemovalListener(); Caffeine<Object, Object> builder = Caffeine.newBuilder().removalListener(testListener); try { // even to the same instance is not allowed builder = builder.removalListener(testListener); fail(); } catch (IllegalStateException expected) {} }
Example #8
Source File: CaffeineUtils.java From t-io with Apache License 2.0 | 4 votes |
/** * @param cacheName * @param timeToLiveSeconds 设置写缓存后过期时间(单位:秒) * @param timeToIdleSeconds 设置读缓存后过期时间(单位:秒) * @param initialCapacity * @param maximumSize * @param recordStats * @param removalListener * @return */ public static <K, V> LoadingCache<K, V> createLoadingCache(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds, Integer initialCapacity, Integer maximumSize, boolean recordStats, RemovalListener<K, V> removalListener) { if (removalListener == null) { removalListener = new DefaultRemovalListener<K, V>(cacheName); } Caffeine<K, V> cacheBuilder = Caffeine.newBuilder().removalListener(removalListener); //设置并发级别为8,并发级别是指可以同时写缓存的线程数 // cacheBuilder.concurrencyLevel(concurrencyLevel); if (timeToLiveSeconds != null && timeToLiveSeconds > 0) { //设置写缓存后8秒钟过期 cacheBuilder.expireAfterWrite(timeToLiveSeconds, TimeUnit.SECONDS); } if (timeToIdleSeconds != null && timeToIdleSeconds > 0) { //设置访问缓存后8秒钟过期 cacheBuilder.expireAfterAccess(timeToIdleSeconds, TimeUnit.SECONDS); } //设置缓存容器的初始容量为10 cacheBuilder.initialCapacity(initialCapacity); //设置缓存最大容量为100,超过100之后就会按照LRU最近最少使用算法来移除缓存项 cacheBuilder.maximumSize(maximumSize); if (recordStats) { //设置要统计缓存的命中率 cacheBuilder.recordStats(); } //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 LoadingCache<K, V> loadingCache = cacheBuilder.build(new CacheLoader<K, V>() { @Override public V load(K key) throws Exception { return null; } }); return loadingCache; // for (int i = 0; i < 20; i++) // { // //从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据 // Long student = studentCache.get("p"); // System.out.println(student); // //休眠1秒 // TimeUnit.SECONDS.sleep(1); // } // System.out.println("cache stats:"); //最后打印缓存的命中率等 情况 // System.out.println(studentCache.stats().toString()); }
Example #9
Source File: CacheBuilderGwtTest.java From caffeine with Apache License 2.0 | 4 votes |
public void testRemovalListener() { final int[] stats = new int[4]; RemovalListener<Integer, Integer> countingListener = new RemovalListener<Integer, Integer>() { @Override public void onRemoval(Integer key, Integer value, RemovalCause cause) { switch (cause) { case EXPIRED: stats[0]++; break; case EXPLICIT: stats[1]++; break; case REPLACED: stats[2]++; break; case SIZE: stats[3]++; break; default: throw new IllegalStateException("No collected exceptions in GWT CacheBuilder."); } } }; Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .expireAfterWrite(1000, TimeUnit.MILLISECONDS) .executor(MoreExecutors.directExecutor()) .removalListener(countingListener) .initialCapacity(100) .ticker(fakeTicker::read) .maximumSize(2)); // Enforce full initialization of internal structures cache.putAll(ImmutableMap.of(1, 1, 2, 2, 3, 3)); cache.invalidateAll(); Arrays.fill(stats, 0); // Add more than two elements to increment size removals. cache.put(1, 10); cache.put(2, 20); cache.put(3, 30); cache.put(4, 40); cache.put(5, 50); // Replace the two present elements. Integer key1 = Iterables.get(cache.asMap().keySet(), 0); Integer key2 = Iterables.get(cache.asMap().keySet(), 1); cache.put(key1, 60); cache.put(key2, 70); cache.put(key1, 80); cache.put(key2, 90); // Expire the two present elements. key1 = Iterables.get(cache.asMap().keySet(), 0); key2 = Iterables.get(cache.asMap().keySet(), 1); fakeTicker.advance(1001, TimeUnit.MILLISECONDS); cache.getIfPresent(key1); cache.getIfPresent(key2); // Add two elements and invalidate them. cache.put(6, 100); cache.put(7, 200); cache.invalidateAll(); assertEquals(2, stats[0]); assertEquals(2, stats[1]); assertEquals(4, stats[2]); assertEquals(3, stats[3]); }
Example #10
Source File: CacheContext.java From caffeine with Apache License 2.0 | 4 votes |
public RemovalListener<Integer, Integer> removalListener() { return requireNonNull(removalListener); }
Example #11
Source File: RemovalListeners.java From caffeine with Apache License 2.0 | 4 votes |
/** A removal listener that throws an exception if a notification arrives. */ public static <K, V> RemovalListener<K, V> rejecting() { return new RejectingRemovalListener<>(); }