Skip to content

Is it possible to setup a dynamic expiry check for each entry? #1786

Answered by ben-manes
SidB3 asked this question in Q&A
Discussion options

You must be logged in to vote

This is where refreshAfterWrite is useful. If the entry is accessed after that duration, but before expiration, then an async refresh is performed. The CacheLoader.reload(key, oldValue) can load the new value, optionally returning the old one is desired. This is used to hide latency rather than allow popular entries to expire and cause requests to stall waiting for it to be loaded anew.

If you do want to resurrect at eviction time then this can be achieved in a roundabout manner using a listener that reinserts the entry. The Caffeine.removalListener is called after the operation completes, so it could reinsert with a short race if a concurrent load happens concurrently. That minor problem…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@SidB3
Comment options

Answer selected by ben-manes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants