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