mjs
mjs

Reputation: 22347

Google Guava Cache, refreshing the expire timeout

I have the following, which sets a default expire timeout for all new entries.

Cache<String, Tracker> cache = CacheBuilder.newBuilder().
expireAfterAccess( expire , TimeUnit.MINUTES ).build();

How can I refresh the expire access, after it has been populated?

Of course, one solution might be to remove the item and reinsert it, but that might be more expensive than what is already possible in other ways.

Upvotes: 1

Views: 6263

Answers (1)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 27995

You should just access the element you want to refresh using cache.getIfPresent(key). According to Guava wiki page (emphasis mine):

expireAfterAccess(long, TimeUnit) Only expire entries after the specified duration has passed since the entry was last accessed by a read or a write. Note that the order in which entries are evicted will be similar to that of size-based eviction.

so it should do exactly what you want to achieve.

Upvotes: 4

Related Questions