简体   繁体   English

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

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

For instance, I configure the cache the following way: 例如,我按以下方式配置缓存:

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;     
}

Now what if later I would like to find out the duration of the expiry policy from the returned igniteCache. 现在如果以后我想从返回的igniteCache中找出到期策略的持续时间。 I can do it in the following hacky way, but it is ugly and can not be the right way: 我可以用下面的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;
    }
}

The Apache Ignite version I'm working on is 1.5.0.final 我正在研究的Apache Ignite版本是1.5.0.final

By the way, in Ehcache, I can simply get the configuration the following way: 顺便说一句,在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;
    }
}

Presently I don't see any other way to get the TTL value for cache entries with the public API. 目前我没有看到任何其他方法来获取公共API的缓存条目的TTL值。 However if such a helper method existed, most likely it would be implemented in a similar way you did but it would calculate the TTL only once returning the same value for following method's calls. 但是,如果存在这样的辅助方法,很可能它会以类似的方式实现,但只有一次为后续方法的调用返回相同的值时才会计算TTL。

So you can simply improve your code by calculating the TTL only once and it would become more optimal. 因此,您只需通过计算一次TTL就可以简单地改进代码,它将变得更加优化。 Also you can always reach out Ignite community proposing to add similar methods to the public API. 此外,您可以随时联系Ignite社区,建议在公共API中添加类似的方法。

Followed dmagda's suggestion (cheers), I reached out to the Ignite community. 按照dmagda的建议(干杯),我联系了Ignite社区。 As advised by AndreyVel from the Apache Ignite Users forum , currently we can get ExpiryPolicy without hacking: 正如Apache Ignite用户论坛的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;
    }
}

Also a comment from alexey.goncharuk at the forum, that he agrees that this way is a bit awkward and it only covers a configured ExpiryPolicy, currently there is no way to check if an instance of IgniteCache was created using withExpiryPolicy() method. 来自论坛的alexey.goncharuk的评论,他同意这种方式有点尴尬,它只涵盖配置的ExpiryPolicy,目前无法检查是否使用withExpiryPolicy()方法创建了一个IgniteCache实例。

He think it should be ok to add getExpiryPolicy() on IgniteCache which will return a configured expiry policy (possibly null) if this is a default cache instance, and user-specified expiry policy if cache instance was created using withExpiryPolicy(). 他认为可以在IgniteCache上添加getExpiryPolicy(),如果这是默认缓存实例,则返回已配置的到期策略(可能为null),如果使用withExpiryPolicy()创建缓存实例,则返回用户指定的到期策略。

Cross-posting this to Ignite dev list to see what dev community thinks. 将此交叉发布到Ignite开发列表,以查看开发社区的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM