Kevin
Kevin

Reputation: 6292

Implementing cache using Guava

I want to implement a cache using Guava's caching mechanism.

I have a DB query which returns a map, I want to cache the entire map but let it expire after a certain amount of time.

I realize Guava caches works as a per-item bases. We provide a key, the Cache will either returns the corresponding value from the cache or get it.

Is there a way to use Guava to get everything, cache it but timeout it after a certain time period of time and get everything again.

Many thanks

Upvotes: 1

Views: 1916

Answers (1)

David
David

Reputation: 3113

You can create an instance of Supplier<Map<K,V>> that fetches the entire map from the database, and then use Suppliers.memoizeWithExpiration to cache it.

Related:

Upvotes: 2

Related Questions