Yuci Yuci - 4 months ago 58
Java Question

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

Answer

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.