Caffeine每个键到期用法示例

huangapple 未分类评论70阅读模式
英文:

Example of Caffeine per key expiryusage

问题

以下是翻译的内容:

在Caffeine中如何使用每个键的到期设置是否有示例?

我看到了以下示例 - 这是否意味着我们要为每个键创建一个Caffeine缓存实例?

链接到GitHub Issue评论

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_VALUE;
  }
  public long expireAfterRead(Key key, Graph graph,
      long currentTime, long currentDuration) {
    return Long.MAX_VALUE;
  }
})
.build();

现在,每当我执行以下操作时 - 每个键插入都将启用键到期:

cache.put(key, graph); // 这里未显示键和图的创建。
英文:

Are there any examples of how to use per key expiry in Caffeine?

I see the following example -- does it mean we are create a Caffeine cache instance per key?

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));

I looked at the implementation and see how the implementation of the expiry interface is used internally.

So say my graph object had a method for expiry duration .. would this be a correct usage?

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();

Now any time I do something like the following -- per key expiry will be enabled for the key inserted -

cache.put(key, graph); // key and graph creation not shown here.

huangapple
  • 本文由 发表于 2020年8月15日 01:30:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/63417496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定