java - 每个关键过期时间的咖啡因示例
问题描述
是否有任何关于如何在咖啡因中使用每个密钥到期的示例?
我看到下面的例子——这是否意味着我们要为每个键创建一个 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.
解决方案
推荐阅读
- c - 日期很旧的当地时间
- sql - How to increment nvarchar value by 1
- javascript - 如何从变量向 JSON 对象添加索引
- html - 将 flex 容器内的子项调整为内联
- javascript - 无法将 javascript 添加到 html
- karate - 在空手道中,创建的 surefire XML 报告在执行的下一个测试中一个接一个地为所有示例附加和合并测试步骤
- javascript - 如何更改 HTML 标签跨度中的文本?
- javascript - 在 mongoDB 中搜索集合时如何接受空数据
- identityserver4 - 声明未添加到 Identity Server 4 中的令牌
- javascript - jQuery .click() 用于附加元素