I’m a little confused by CacheBuilder and Cache introduced in Guava 10. The documentation hints that it’s possible to overwrite values but as far as I can tell, Cache does not contain any methods for doing so. Any ideas?
I’m trying to construct a Map that expires a key 10 seconds after it was last read or written-to. When a value is looked up, I expect the previously-set value to be returned, or a default value to be computed if none exists.
NOTE: This question is outdated. Although the above Javadoc shows the existence of a Cache.put(K key, V value) method, it not exist when the question was first posted.
Since long, there’s
Cache#asMapreturning aConcurrentMapview.AFAIK, not yet. But there’s a thread mentioning thatCache.asMap.putis planned for release 11.I’d say the
currentold state of the Javadoc is a remnant if theCacheBuilder‘s evolution from theMapMaker(where the cache-setting method are currently deprecated).Using
expireAfterAccess(10, TimeUnit.SECONDS)will keep an entry alive for 10 seconds after any access to it. And the only values you’ll get are those computed by yourCacheLoader(either earlier or duringget).