use of javax.cache.configuration.MutableConfiguration in project Payara by payara.
the class JSR107Producer method createCache.
/**
* Produces a Cache for injection. If the @NamedCache annotation is present the
* cache will be created based on the values of the annotation field
*
* Otherwise the cache will be created with the cache name being equal
* to the fully qualified name of the class into which it is injected
* @param ip
* @return
*/
@Produces
public <K, V> Cache<K, V> createCache(InjectionPoint ip) {
Cache<K, V> result;
if (!hazelcastCore.isEnabled()) {
logger.warning("Unable to inject Cache as Hazelcast is Disabled");
return null;
}
// determine the cache name first start with the default name
String cacheName = ip.getMember().getDeclaringClass().getCanonicalName();
NamedCache ncqualifier = ip.getAnnotated().getAnnotation(NamedCache.class);
CacheManager manager = getCacheManager(ip);
if (ncqualifier != null) {
// configure the cache based on the annotation
String qualifierName = ncqualifier.cacheName();
if (!"".equals(cacheName)) {
cacheName = qualifierName;
}
Class keyClass = ncqualifier.keyClass();
Class valueClass = ncqualifier.valueClass();
result = manager.getCache(cacheName, keyClass, valueClass);
if (result == null) {
MutableConfiguration<K, V> config = new MutableConfiguration<>();
config.setTypes(keyClass, valueClass);
// determine the expiry policy
Class expiryPolicyFactoryClass = ncqualifier.expiryPolicyFactoryClass();
if (!"Object".equals(expiryPolicyFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(expiryPolicyFactoryClass);
config.setExpiryPolicyFactory(factory);
}
// determine the cache writer if any
Class writerFactoryClass = ncqualifier.cacheWriterFactoryClass();
if (!"Object".equals(writerFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(writerFactoryClass);
config.setCacheWriterFactory(factory);
}
config.setWriteThrough(ncqualifier.writeThrough());
// determine the cache loader if any
Class loaderFactoryClass = ncqualifier.cacheLoaderFactoryClass();
if (!"Object".equals(loaderFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(loaderFactoryClass);
config.setCacheLoaderFactory(factory);
}
config.setReadThrough(ncqualifier.readThrough());
config.setManagementEnabled(ncqualifier.managementEnabled());
config.setStatisticsEnabled(ncqualifier.statisticsEnabled());
result = manager.createCache(cacheName, config);
}
} else {
// configure a "raw" cache
Bean<?> bean = ip.getBean();
if (bean != null) {
Class<?> beanClass = bean.getBeanClass();
CacheDefaults defaults = beanClass.getAnnotation(CacheDefaults.class);
if (defaults != null) {
String cacheNameFromAnnotation = defaults.cacheName();
if (!"".equals(cacheNameFromAnnotation)) {
cacheName = cacheNameFromAnnotation;
}
}
}
result = manager.getCache(cacheName);
if (result == null) {
MutableConfiguration<K, V> config = new MutableConfiguration<>();
config.setManagementEnabled(true);
config.setStatisticsEnabled(true);
result = manager.createCache(cacheName, config);
}
}
return result;
}
use of javax.cache.configuration.MutableConfiguration in project wcomponents by BorderTech.
the class HandlebarsCacheImpl method getCache.
/**
* @return the cache instance
*/
protected synchronized Cache<TemplateSource, Template> getCache() {
Cache<TemplateSource, Template> cache = Caching.getCache(CACHE_NAME, TemplateSource.class, Template.class);
if (cache == null) {
final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
MutableConfiguration<TemplateSource, Template> config = new MutableConfiguration<>();
config.setTypes(TemplateSource.class, Template.class);
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
// Handlebars template classes are not serializable so use by ref.
config.setStoreByValue(false);
cache = mgr.createCache(CACHE_NAME, config);
}
return cache;
}
use of javax.cache.configuration.MutableConfiguration in project carbon-apimgt by wso2.
the class CreateCache method execute.
@Override
public BValue[] execute(Context context) {
String cacheName = getStringArgument(context, 0);
String cacheTimeoutString = getStringArgument(context, 1);
// Default cache timeout is 15 minutes
int cacheTimeout = 15;
if (cacheTimeoutString != null && cacheTimeoutString.length() > 0) {
cacheTimeout = ((cacheTimeout = Integer.parseInt(cacheTimeoutString)) > 0 ? cacheTimeout : 15);
}
CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
if ((cacheManager).getCache(cacheName) == null) {
Duration cacheExpiry = new Duration(TimeUnit.MINUTES, cacheTimeout);
MutableConfiguration<String, String> config = new MutableConfiguration<>();
config.setStoreByValue(true).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(cacheExpiry)).setStatisticsEnabled(false).setStoreByValue(false);
cacheManager.createCache(cacheName, config);
}
return getBValues(new BString(cacheName));
}
use of javax.cache.configuration.MutableConfiguration in project camel by apache.
the class JCacheManager method getOrCreateCacheConfiguration.
private Configuration getOrCreateCacheConfiguration() {
if (configuration.getCacheConfiguration() != null) {
return configuration.getCacheConfiguration();
}
MutableConfiguration mutableConfiguration = new MutableConfiguration();
if (configuration.getCacheLoaderFactory() != null) {
mutableConfiguration.setCacheLoaderFactory(configuration.getCacheLoaderFactory());
}
if (configuration.getCacheWriterFactory() != null) {
mutableConfiguration.setCacheWriterFactory(configuration.getCacheWriterFactory());
}
if (configuration.getExpiryPolicyFactory() != null) {
mutableConfiguration.setExpiryPolicyFactory(configuration.getExpiryPolicyFactory());
}
mutableConfiguration.setManagementEnabled(configuration.isManagementEnabled());
mutableConfiguration.setStatisticsEnabled(configuration.isStatisticsEnabled());
mutableConfiguration.setReadThrough(configuration.isReadThrough());
mutableConfiguration.setStoreByValue(configuration.isStoreByValue());
mutableConfiguration.setWriteThrough(configuration.isWriteThrough());
return mutableConfiguration;
}
use of javax.cache.configuration.MutableConfiguration in project tutorials by eugenp.
the class EntryProcessorIntegrationTest method instantiateCache.
@Before
public void instantiateCache() {
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
MutableConfiguration<String, String> config = new MutableConfiguration<>();
this.cache = cacheManager.createCache(CACHE_NAME, config);
this.cache.put("key", "value");
}
Aggregations