Monta
Monta

Reputation: 1180

Caffeine Cache - How to get information of creation date of an element

Is there any way to access the creation timestamp of an element from the CaffeineCache? Kind of cache.get("x").getTimestamp()?

Upvotes: 1

Views: 1377

Answers (1)

Ben Manes
Ben Manes

Reputation: 9621

The cache tries to store the minimal metadata required to perform its operations, so some conveniences are left out to avoid waste. In those cases you should consider adding that metadata by wrapping your value.

The cache does expose runtime metadata, but this often depends on how it was constructed. That can be accessed by using Cache.policy(). For example cache.policy().expireAfterWrite() offers an ageOf(key) method to determine how long the entry has resided since its expiration timestamp was last reset. To calculate how much time until the entry expires, you could subtract the age from the policy's duration (via getExpiresAfter()).

Upvotes: 3

Related Questions