Skip to content

RemovalListener Internal working for callback on expireAfterCreate #493

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

You must be logged in to vote

It sounds like you want to use a scheduler to evict the entry when no other activity on the cache is occurring. Please see this wiki page for more details.

LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
    .scheduler(Scheduler.systemScheduler())
    .expireAfterWrite(10, TimeUnit.MINUTES)
    .build(key -> createExpensiveGraph(key));

The Scheduler.systemScheduler() is available in Java 9+ and uses a JVM provided thread, or else you can provide a ScheduledExecutorService. The cache will peek at the next entry to expire and schedule a cleanUp() call.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@pushkardhat
Comment options

@ben-manes
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