Search code examples
javacachingcache2k

Does cache2k put() update the expiry time


I can't find an answer in the API docs, that's why I'd like to ask here:

Given I have a cache2k cache

import org.cache2k.Cache;
import org.cache2k.CacheBuilder;
import java.util.concurrent.TimeUnit;
....

Cache<String, Integer> cache =
  CacheBuilder.newCache(String.class, Integer.class)
    .expiryDuration(1, TimeUnit.MINUTES)

and put an item in it

cache.put("item", 1)

the expiry time of this item is one minute. After 30 seconds I update this item with

cache.put("item", 0)

is the expiry time of this item now approximatly 30 second or one minute again?

Every helpful comment is very appreciated. Maybe I missed the answer in the docs or there is some standard I don't know about...

Regards

globalworming


Solution

  • The expiry duration is the time between an entry is stored in the cache (e.g. via put or a get() and read through) until the entry is considered expired. Expired entries are no longer valid and not returned any more by the cache. Whether an expired entry is removed from the internal cache data depends on other configurations.

    Some caches make a difference between the expiry time after a new entry and an update. In cache2k this is identical.

    So, after the second put() the expiry is reset and the entry expires after one minute.