在运行时使用infinispan获取TTL

8
我正在使用Infinispan缓存。是否有任何方法可以在运行时获取条目的TTL(或生命周期)?我看到接口CacheEntry作为getLifespan() API,但我不知道如何处理CacheEntry接口,
谢谢

你试过我的答案了吗?如果不起作用,请告诉我。 - Braj
2个回答

8

要获取整个缓存的生命周期配置,您可以使用以下命令:

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

要获取特定条目的生命周期,可以使用以下方法:

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

希望这能帮到您!

谢谢,但我只能在AdvanceCache上看到getEntry(Object,EnumSet,ClassLoader)方法。还有其他想法吗? - Breako Breako
1
如果您使用的是Infinispan < 5.3,则getCacheEntry(K key)实际上不存在,但getCacheEntry(key,null,null)应该可以检索相同的结果。 - Radim Vansa

2
每个缓存条目包含以下信息:
  • 最后使用时间
  • 最大空闲时间
  • 过期时间
其中 过期时间 = 最大空闲时间 + 最后使用时间 使用这些信息来获取每个缓存条目的生命周期。
方法 CacheEntry.getLifeSpan() 并不能按预期工作,它返回 -1,表示无限寿命。
以下是示例代码:
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");
    }

}

输出:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接