【问题标题】:Get the TTL at runtime with infinispan使用 infinispan 在运行时获取 TTL
【发布时间】:2014-03-31 15:47:18
【问题描述】:

我正在使用 Infinispan 缓存。有没有办法在运行时获取条目的 TTL(或 lifepsan)?我将接口CacheEntry 视为getLifespan() API,但我看不到如何获取 CacheEntry 接口的句柄,

谢谢

【问题讨论】:

  • 你有没有试过我的答案。如果它不起作用,请告诉我。

标签: infinispan


【解决方案1】:

要获取整个缓存的寿命配置,您可以使用:

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

要获取特定条目的寿命,您可以使用:

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

希望有帮助!

【讨论】:

  • 谢谢,但我只能在 AdvanceCache 上看到 getEntry(Object, EnumSet, ClassLoader)。还有其他想法吗?
  • 如果您使用的是 Infinispan
【解决方案2】:

每个缓存条目包含以下信息:

  • 上次使用
  • 最大空闲
  • 到期时间

在哪里Expiry Time = Max Idle + Last Used

使用此信息获取每个缓存条目的生命周期。

方法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

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    相关资源
    最近更新 更多