0

javadoc toexpireAfterAccess方法说:

指定在条目创建、最近一次替换其值或最后一次访问后经过固定持续时间后,应自动从缓存中删除每个条目。访问时间由所有缓存读取和写入操作(包括 Cache.asMap().get(Object) 和 Cache.asMap().put(K, V))重置,但不通过对 Cache 的集合视图的操作重置。地图

我有以下代码:

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 中的粗体短语意味着什么?

4

1 回答 1

3

operations on the collection-views of Cache.asMap

The collections exposed by Map that are views on the Cache are keySet(), values(), entrySet(). Iterating on any of those won't reset the access time, and neither will writing the value of a Map.Entry obtained through entrySet().

于 2013-06-20T19:45:10.057 回答