Skip to content

A simple API to specify an expiration time for individual entries while inserting is needed #1303

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

You must be logged in to vote

It is a little tougher in Caffeine because we have to balance more features and higher performance, while also trying to keep the api simple to use. The expireAfter configures the cache to support variable expiration and the policy() apis gives feature-specific methods for more advanced usages. The below examples uses a default policy where a new entry never expires if not set on creation and honors the remaining lifetime on every subsequent read or write. The put(k, v, duration) then create or updates the entry to set a new expiration time.

Cache<String, Connection> cache = Caffeine.newBuilder()
    .expireAfter(new Expiry<String, Connection>() {
      public long expireAfterCreate(String 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
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