@Override public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Configuration<K, V> config) { Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass); if (cache == null) { final CacheManager mgr = Caching.getCachingProvider().getCacheManager(); cache = mgr.createCache(name, config); } return cache; }
@Override public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Configuration<K, V> config) { Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass); if (cache == null) { final CacheManager mgr = Caching.getCachingProvider().getCacheManager(); cache = mgr.createCache(name, config); } return cache; }
@Test public void cachingProviderGetNonExistentCache() { String name = "nonExistentCache"; Cache cache = Caching.getCache(name, Long.class, String.class); assertNull(null, cache); }
@Override public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Duration duration) { Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass); if (cache == null) { final CacheManager mgr = Caching.getCachingProvider().getCacheManager(); MutableConfiguration<K, V> config = new MutableConfiguration<>(); config.setTypes(keyClass, valueClass); config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration)); cache = mgr.createCache(name, config); } return cache; }
@Override public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Duration duration) { Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass); if (cache == null) { final CacheManager mgr = Caching.getCachingProvider().getCacheManager(); MutableConfiguration<K, V> config = new MutableConfiguration<>(); config.setTypes(keyClass, valueClass); config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration)); cache = mgr.createCache(name, config); } return cache; }
/** * @return the cache instance */ protected synchronized Cache<Object, Resource> getCache() { Cache<Object, Resource> cache = Caching.getCache(CACHE_NAME, Object.class, Resource.class); if (cache == null) { final CacheManager mgr = Caching.getCachingProvider().getCacheManager(); MutableConfiguration<Object, Resource> config = new MutableConfiguration<>(); config.setTypes(Object.class, Resource.class); config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12))); // Velocity template classes are not serializable so use by ref. config.setStoreByValue(false); cache = mgr.createCache(CACHE_NAME, config); } return cache; }
/** * @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; }
/** * @return the cache instance */ protected synchronized Cache<String, String> getCache() { Cache<String, String> cache = Caching.getCache(CACHE_NAME, String.class, String.class); if (cache == null) { final CacheManager mgr = Caching.getCachingProvider().getCacheManager(); MutableConfiguration<String, String> config = new MutableConfiguration<>(); config.setTypes(String.class, String.class); config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12))); // No need to serialize the result config.setStoreByValue(false); cache = mgr.createCache(CACHE_NAME, config); } return cache; }
@Test(expected = NullPointerException.class) public void getCacheNullValueClass() { String name = "c1"; CacheManager manager = Caching.getCachingProvider().getCacheManager(); manager.createCache(name, new MutableConfiguration().setTypes(Long.class, String.class)); try { Caching.getCache(name, Long.class, null); } finally { manager.destroyCache(name); } }
@Test public void cachingProviderGetCache() { String name = "c1"; getCacheManager().createCache(name, new MutableConfiguration().setTypes(Long.class, String.class)); Cache cache = Caching.getCache(name, Long.class, String.class); assertEquals(name, cache.getName()); }
@Test public void cachingProviderGetCache() { String name = "c1"; Caching.getCachingProvider().getCacheManager().createCache(name, new MutableConfiguration().setTypes(Long.class, String.class)); Cache cache = Caching.getCache(name, Long.class, String.class); assertEquals(name, cache.getName()); Caching.getCachingProvider().getCacheManager().destroyCache(name); }