Get the TTL at runtime with infinispan
Asked Answered
M

2

8

I am using the Infinispan cache. Is there any way to get the TTL (or lifepsan) of entries at runtime? I see the interface CacheEntry as a getLifespan() API but I don't see how to get a handle on CacheEntry interface,

Thanks

Mcduffie answered 27/2, 2014 at 22:12 Comment(1)
Have you tries my answer. Please let me know if its not working.Eggplant
S
8

To get the configuration of lifespan for entire cache, you can use:

cache.getCacheConfiguration().expiration().lifespan();

and to obtain lifespan for specific entry, you can use:

cache.getAdvancedCache().getCacheEntry("key").getLifespan();

Hope that helps!

Sarthe answered 4/3, 2014 at 7:26 Comment(2)
Thanks but I can only see getEntry(Object, EnumSet, ClassLoader) on the AdvanceCache. any other ideas?Mcduffie
In case you're using Infinispan < 5.3, getCacheEntry(K key) is really not there, but getCacheEntry(key, null, null) should retrieve the same result.Taffeta
E
2

Each Cache Entry contains information about:

  • Last Used
  • Max Idle
  • Expiry Time

Where Expiry Time = Max Idle + Last Used

Use this information to get the lifespan of each Cache Entry.

Method CacheEntry.getLifeSpan() is not working as expected that retrieves the lifespan of this entry. It returns -1 that means an unlimited lifespan.

Here is a sample code:

import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.container.entries.TransientCacheEntry;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;

public class InfinispanTTL {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("start");

        ConfigurationBuilder confBuilder = new ConfigurationBuilder();
        // confBuilder.eviction().strategy(EvictionStrategy.NONE).maxEntries(3);
        confBuilder.expiration().lifespan(5000);
        confBuilder.clustering().cacheMode(CacheMode.LOCAL);

        EmbeddedCacheManager cacheManager = new DefaultCacheManager(confBuilder.build());
        cacheManager.start();

        Cache<String, CacheEntry> sessionCache = cacheManager.getCache("session");
        System.out.println("Strategy used by container="
                + sessionCache.getCacheConfiguration().eviction().strategy());
        System.out.println("Lifespan of container="
                + sessionCache.getCacheConfiguration().expiration().lifespan());

        TransientCacheEntry cacheEntry = new TransientCacheEntry("a", "1", 1000, 2000);

        System.out.println("Expiry Time = Max Idle + Last Used");
        System.out.println("Max Idle=" + cacheEntry.getMaxIdle());
        System.out.println("Last Used=" + cacheEntry.getLastUsed());
        System.out.println("Expiry Time=" + cacheEntry.getExpiryTime());

        sessionCache.put("a", cacheEntry);

        System.out.println("Expirt Time from session cache="
                + ((TransientCacheEntry) sessionCache.get("a")).getExpiryTime());
        System.out.println("Old value=" + sessionCache.get("a").getValue());
        System.out.println("Set value");
        sessionCache.get("a").setValue("3");
        System.out.println("New value=" + sessionCache.get("a").getValue());

        System.out.println("Expirt Time from session cache="
                + ((TransientCacheEntry) sessionCache.get("a")).getExpiryTime());
        System.out.println("finish");
    }

}

Output:

Strategy used by container=NONE
Lifespan of container=5000
Expiry Time = Max Idle + Last Used
Max Idle=1000
Last Used=2000
Expiry Time=3000
Life span from session cache=-1
Expiry Time from session cache=3000
Old value=1
Set value
New value=3
Expiry Time from session cache=3000
Eggplant answered 7/3, 2014 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.