Reputation: 1180
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
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