如何在 Apache Ignite 中获取给定缓存的配置?

How to get the configuration of a given cache in Apache Ignite?

例如,我按以下方式配置缓存:

public IgniteCache<String, Object> getOrCreateCache(String cacheName) {
    Ignite ignite = Ignition.ignite();

    CacheConfiguration<String, Object> cacheCfg = new CacheConfiguration<String, Object>(cacheName);
    cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 30)));

    IgniteCache<String, Object> igniteCache = ignite.getOrCreateCache(cacheCfg);
    return igniteCache;     
}

现在如果我想从返回的 igniteCache 中找出到期策略的持续时间怎么办。我可以用下面的 hacky 方式来做,但它很丑而且不可能是正确的方式:

import javax.cache.configuration.Factory;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;

import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.CacheConfiguration;

public class IgniteCacheManager {

    private IgniteCache<String, Object> igniteCache;

    public IgniteCacheManager(IgniteCache<String, Object> igniteCache) {
        this.igniteCache = igniteCache;
    }

    public long getTimeToLive() {
        long timeToLive = 0;
        CacheConfiguration configuration = igniteCache.getConfiguration(CacheConfiguration.class);
        Factory factory = configuration.getExpiryPolicyFactory();
        Object obj = factory.create();
        if (obj instanceof CreatedExpiryPolicy) {
            CreatedExpiryPolicy cep = (CreatedExpiryPolicy)obj;
            Duration dur = cep.getExpiryForCreation();
            timeToLive = dur.getDurationAmount();
        }
        return timeToLive;
    }
}

我正在使用的 Apache Ignite 版本是 1.5。0.final

顺便说一句,在Ehcache中,我可以简单地通过以下方式获取配置:

import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;

public class EhcacheCacheManager {

    private Cache cache;

    public EhcacheCacheManager(Cache cache) {
        this.cache = cache;
    }

    public long getTimeToLive() {
        long timeToLive = 0;

        CacheConfiguration config = cache.getCacheConfiguration();
        timeToLive = config.getTimeToLiveSeconds();
        return timeToLive;
    }
}

目前我没有看到任何其他方法来获取 public API 缓存条目的 TTL 值。但是,如果存在这样的辅助方法,它很可能会以与您类似的方式实现,但它只会在为后续方法调用返回相同值时计算 TTL。

所以您可以通过只计算一次 TTL 来简单地改进您的代码,它会变得更加优化。您也可以随时联系 Ignite 社区,提议将类似的方法添加到 public API.

按照 dmagda 的建议(干杯),我联系了 Ignite 社区。正如来自 the Apache Ignite Users forum 的 AndreyVel 所建议的那样,目前我们无需破解即可获得 ExpiryPolicy:

import javax.cache.configuration.Factory;
import javax.cache.expiry.ExpiryPolicy;

import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.CacheConfiguration;

public class IgniteCacheManager {

    private IgniteCache<String, Object> igniteCache;

    public IgniteCacheManager(IgniteCache<String, Object> igniteCache) {
        this.igniteCache = igniteCache;
    }

    public long getTimeToLive() {
        CacheConfiguration configuration = igniteCache.getConfiguration(CacheConfiguration.class);
        Factory factory = configuration.getExpiryPolicyFactory();
        ExpiryPolicy policy = factory.create(); 
        long timeToLive = policy.getExpiryForCreation().getDurationAmount();
        return timeToLive;
    }
}

还有论坛上alexey.goncharuk的评论,他同意这种方式有点笨拙,而且它只涵盖配置的ExpiryPolicy,目前无法检查是否创建了IgniteCache实例使用 withExpiryPolicy() 方法。

他认为在 IgniteCache 上添加 getExpiryPolicy() 应该没问题,如果这是默认缓存实例,它将 return 配置过期策略(可能为 null),如果是缓存实例,则为用户指定的过期策略是使用 withExpiryPolicy() 创建的。

将此交叉发布到 Ignite 开发列表以查看 dev community 的想法。