br.eti.clairton.jcachepolicy.ExpiryCacheResolver Maven / Gradle / Ivy
package br.eti.clairton.jcachepolicy;
import static javax.cache.expiry.AccessedExpiryPolicy.factoryOf;
import java.lang.annotation.Annotation;
import java.util.concurrent.TimeUnit;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.annotation.CacheInvocationContext;
import javax.cache.annotation.CacheResolver;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.Factory;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;
public abstract class ExpiryCacheResolver implements CacheResolver{
protected final CacheManager manager;
public ExpiryCacheResolver(final CacheManager manager) {
this.manager = manager;
}
@Override
public Cache resolveCache(final CacheInvocationContext extends Annotation> context) {
final String name = context.getCacheName();
return getCache(name);
}
protected Duration getDuration() {
return new Duration(getTimeUnit(), getQuantity());
}
protected Factory getFactory() {
return factoryOf(getDuration());
}
protected Configuration getConfiguration(final String name) {
final MutableConfiguration configuration = new MutableConfiguration();
configuration.setExpiryPolicyFactory(getFactory());
return configuration;
}
protected Cache createCache(final String name) {
final Configuration configuration = getConfiguration(name);
return manager.createCache(name, configuration);
}
private synchronized Cache getCache(final String name) {
final Cache cache = manager.getCache(name);
if (cache == null) {
return createCache(name);
}
return cache;
}
abstract TimeUnit getTimeUnit();
abstract Integer getQuantity();
}