Caffeine具有查看上次写入时间的expiresAfterWrite方法。我想让它只看创建的时候。因此,当第一个条目到来时,条目将在一段固定时间后过期,而不会查看此条目上的更新次数。这个是可能的吗?
发布于 2018-08-10 14:17:57
可以,但需要使用更高级的Expiry应用编程接口。在下面的示例中,新创建的条目具有固定的5分钟生存期。这是通过在更新或读取时返回currentDuration来完成的。
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));https://stackoverflow.com/questions/51779879
复制相似问题