Search code examples
cache2k

How to set expiry while put() using cache2k


I want something like redis set and ttl, such as

public void put(K key, V value, int timeToLive, TimeUnit timeUnit); public long ttl(K key);

and one more operation that redis doesn't provide

public void putWithIdle(K key, V value, int timeToIdle, TimeUnit timeUnit);


Solution

  • There is no direct API for this, but after puting the value, you can change the expiry time as described here https://cache2k.org/docs/1.0/user-guide.html#entry-processor

    cache.invoke("key", e -> e.setValue("value").setExpiry(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(120)));

    Consider creating a feature request at https://github.com/cache2k/cache2k/issues