简体   繁体   中英

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. I can do it in the following hacky way, but it is ugly and can not be the right way:

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

By the way, in Ehcache, I can simply get the configuration the following way:

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. 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.

So you can simply improve your code by calculating the TTL only once and it would become more optimal. Also you can always reach out Ignite community proposing to add similar methods to the public API.

Followed dmagda's suggestion (cheers), I reached out to the Ignite community. As advised by AndreyVel from the Apache Ignite Users forum , currently we can get ExpiryPolicy without hacking:

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.

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().

Cross-posting this to Ignite dev list to see what dev community thinks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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