是否有任何关于如何在咖啡因中使用每个密钥到期的示例?
我看到下面的例子——这是否意味着我们要为每个键创建一个 Caffeine 缓存实例?
https://github.com/ben-manes/caffeine/issues/114#issuecomment-300602200
Caffeine.newBuilder()
.expireAfter(new Expiry<Key, Graph>() {
public long expireAfterCreate(Key key, Graph graph, long currentTime) {
return (graph instanceof NullGraph)
? TimeUnit.MINUTES.toNanos(1)
: TimeUnit.MINUTES.toNanos(10);
}
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));
我查看了实现,看看内部如何使用过期接口的实现。
所以说我的图形对象有一个到期时间的方法..这是一个正确的用法吗?
final Cache<Key, Graph> cache = Caffeine.newBuilder()
.expireAfter(new Expiry<Key, Graph>() {
public long expireAfterCreate(Key key, Graph graph, long currentTime) {
return currentTime + graph.expireAfterNanos();
}
public long expireAfterUpdate(Key key, Graph graph,
long currentTime, long currentDuration) {
return Long.Max;
}
public long expireAfterRead(Key key, Graph graph,
long currentTime, long currentDuration) {
return Long.Max;
}
})
.build();
现在,任何时候我执行以下操作——每次密钥到期都会为插入的密钥启用——
cache.put(key, graph); // key and graph creation not shown here.