LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
.expireAfter(new Expiry<Key, Graph>() {
public long expireAfterCreate(Key key, Graph graph, long currentTime) {
return TimeUnit.MINUTES.toNanos(5);
}
public long expireAfterUpdate(Key key, Graph graph,
long currentTime, long currentDuration) {
return currentDuration;
}
public long expireAfterRead(Key key, Graph graph,
long currentTime, long currentDuration) {
return currentDuration;
}
})
.build(key -> createExpensiveGraph(key));
# 1 楼答案
是的,但是需要使用更高级的
Expiry
api。在下面的示例中,新创建的条目具有固定的5分钟生存期。这是通过在更新或读取时返回currentDuration
来完成的