简体   繁体   English

如何在番石榴中将ExpireAfterAccess与CacheBuilder一起使用

[英]how to use expireAfterAccess with CacheBuilder in guava

I have the following code: 我有以下代码:
new MapMaker().expireAfterAccess(SESSION_EXPIRATION, TimeUnit.MILLISECONDS).makeMap();

In guava 10 it said it is deprecated and should be replaced by CacheBuilder . 在番石榴10中,它说它已被弃用,应该由CacheBuilder代替。 What is the new syntax for this? 新的语法是什么?

Reading the documentation, I think you might just use the similar methods, and pass a loader which always throws an exception. 阅读文档,我认为您可能只使用类似的方法,并传递总是会引发异常的加载程序。 You would then call asMap() to get a map, where you would be able to put your keys and values and have it backed by the cache. 然后,您将调用asMap()来获取地图,在该地图中您可以放置​​键和值并由缓存支持。

新语法看起来像

Cache<K, V> cache = CacheBuilder.newBuilder().expireAfterAccess(SESSION_EXPIRATION, TimeUnit.MILLISECONDS).build();

Guava release 11.0 will feature CacheBuilder.build() : Guava 11.0版将具有CacheBuilder.build()

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html

Until then it is unfortunately necessary to use asMap() . 在此之前,不幸的是必须使用asMap()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM