gpt4 book ai didi

java - Guava 在 Collection View 上的 expireAfterAccess 缓存

转载 作者:行者123 更新时间:2023-12-01 23:45:02 24 4
gpt4 key购买 nike

expireAfterAccess 方法的 javadoc 表示:

Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last access. Access time is reset by all cache read and write operations (including Cache.asMap().get(Object) and Cache.asMap().put(K, V)), but not by operations on the collection-views of Cache.asMap

我有以下代码:

Cache<String, String> cache = CacheBuilder.newBuilder()
.expireAfterAccess(2, TimeUnit.SECONDS)
.build();
ConcurrentMap<String, String> map = cache.asMap();
map.put("a", "12345");
System.out.println("First access: " + map.get("a"));
System.out.println("Second access: " + map.get("a"));
Thread.sleep(1900); //1.9 seconds
System.out.println("Third access: " + map.get("a"));
Thread.sleep(1000); //1 second
System.out.println("Fourth access: " + map.get("a"));
Thread.sleep(1500); //1.5 second
System.out.println("Fivth access: " + map.get("a"));

它的输出是:

First access: 12345
Second access: 12345
Third access: 12345
Fourth access: 12345
Fivth access: 12345

因此,正如我们所看到的,当我们对 Collection View 执行 get 操作时,访问时间也会重置。在这种情况下,javadoc 中的粗体短语意味着什么?

最佳答案

operations on the collection-views of Cache.asMap

Map 公开的集合(即 Cache 上的 View )是 keySet()values()entrySet()。迭代其中任何一个都不会重置访问时间,写入通过 entrySet() 获取的 Map.Entry 的值也不会重置。

关于java - Guava 在 Collection View 上的 expireAfterAccess 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17218444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com