本文介绍了是否有针对咖啡因缓存的CacheEntryExpiredListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 Cache2k
具有一个 CacheEntryExpiredListener
,只有在缓存条目自到期时才触发(不是在显式失效时才触发).
I know Cache2k
having a CacheEntryExpiredListener
that is only triggered if a cache entry self-expires (not when being invalidated explicit).
Cache<String, Object> cache = Cache2kBuilder.of(String.class, Object.class)
.addListener(
(CacheEntryExpiredListener<String, Object>) (cache, entry)
-> handleExpired(entry.getKey(), entry.getValue()))
.expireAfterWrite(60, TimeUnit.SECONDS)
.build();
问题:如何使用 caffeine
缓存实现实现相同的目的?
Question: how could I achieve the same using caffeine
cache implementation?
推荐答案
感谢@Ben Manes的提示:
Thanks to the hint from @Ben Manes:
Caffeine.newBuilder()
.removalListener((key, value, cause) -> {
if (cause.wasEvicted()) System.out.printf("key=%s, value=%s", key, value);
})
.expireAfterWrite(60, TimeUnit.SECONDS)
.build();
这篇关于是否有针对咖啡因缓存的CacheEntryExpiredListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!