Skip to content

How to Enable Cache to Set Changing Time to Lives #1834

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

You must be logged in to vote

You can use a default TTL that is essentially eternity, then use the VarExpiration api to put the entry in with an explicit duration.

Cache<Key, Graph> graphs = Caffeine.newBuilder()
    .expireAfter(Expiry.creating((Key key, Graph graph) -> Duration.ofNanos(Long.MAX_VALUE)))
    .build();
graphs.policy().expireVariably().orElseThrow()
    .put(key, value, Duration.ofMinutes(30));

wdyt?

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ben-manes
Comment options

@rickilambert
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