/** See {@link MutableConfiguration#setTypes}. */ public void setTypes(Class<K> keyType, Class<V> valueType) { delegate.setTypes(keyType, valueType); }
/** {@inheritDoc} */ @Override public CacheConfiguration<K, V> setTypes(Class<K> keyType, Class<V> valType) { super.setTypes(keyType, valType); return this; }
protected <K, T> Cache<K, SerializedEntity<T>> createCache(String cacheName, Type<T> type) { Class keyClass = getKeyClass(type); if (keyClass == null) { throw new IllegalStateException(); } MutableConfiguration<K, SerializedEntity<T>> configuration = new MutableConfiguration<>(); configuration.setTypes(keyClass, (Class) SerializedEntity.class); configure(configuration); return cacheManager.createCache(cacheName, configuration); }
<K, V> void createCache(final CacheManager cacheManager, final String cacheName, final Class<K> keyType, final Class<V> valueType) { final Configuration jcacheConfig = new MutableConfiguration().setTypes(keyType, valueType); final Configuration redissonConfiguration = RedissonConfiguration.fromInstance(redissonClient, jcacheConfig); createCache(cacheManager, cacheName, redissonConfiguration); } }
<K, V> void createCache(final CacheManager cacheManager, final String cacheName, final Class<K> keyType, final Class<V> valueType) { // All other configuration options come from the ehcache.xml final Configuration configuration = new MutableConfiguration<K, V>().setTypes(keyType, valueType) .setStoreByValue(false); // Store by reference to avoid copying large objects (e.g. catalog) super.createCache(cacheManager, cacheName, configuration); } }
@Test public void testTemplateAddsListeners() throws Exception { CacheManager cacheManager = cachingProvider.getCacheManager(getClass().getResource("/ehcache-107-listeners.xml") .toURI(), getClass().getClassLoader()); MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(Test107CacheEntryListener::new, null, false, true); configuration.addCacheEntryListenerConfiguration(listenerConfiguration); Cache<String, String> cache = cacheManager.createCache("foos", configuration); cache.put("Hello", "Bonjour"); assertThat(Test107CacheEntryListener.seen.size(), Matchers.is(1)); assertThat(TestCacheEventListener.seen.size(), Matchers.is(1)); }
@Test public void test107ExpiryOverriddenByEhcacheTemplateExpiry() { final AtomicBoolean expiryFactoryInvoked = new AtomicBoolean(false); MutableConfiguration<Long, Product> configuration = new MutableConfiguration<>(); configuration.setTypes(Long.class, Product.class); configuration.setExpiryPolicyFactory(() -> { expiryFactoryInvoked.set(true); return new CreatedExpiryPolicy(Duration.FIVE_MINUTES); }); cacheManager.createCache("productCache3", configuration); assertThat(expiryFactoryInvoked.get(), is(false)); }
@BeforeClass public void beforeClass() { final CachingProvider provider = Caching.getCachingProvider(PROVIDER_NAME); cacheManager = provider.getCacheManager(); cacheManager.destroyCache("cache-not-in-config-file"); cacheConfig = new MutableConfiguration<>(); cacheConfig.setTypes(String.class, String.class); cacheConfig.setStatisticsEnabled(true); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); CachingProvider provider = Caching.getCachingProvider(); cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-loader-writer-107.xml").toURI(), getClass().getClassLoader()); testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>() .setReadThrough(true) .setWriteThrough(true) .setCacheLoaderFactory(() -> cacheLoader) .setCacheWriterFactory(() -> cacheWriter) .setTypes(Number.class, CharSequence.class)); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); CachingProvider provider = Caching.getCachingProvider(); cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-loader-writer-107-load-atomics.xml").toURI(), getClass().getClassLoader()); testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>() .setReadThrough(true) .setWriteThrough(true) .setCacheLoaderFactory(() -> cacheLoader) .setCacheWriterFactory(() -> cacheWriter) .setTypes(Number.class, CharSequence.class)); }
@Test public void setWriteThroughWithoutWriterFails() { MutableConfiguration<Long, String> config = new MutableConfiguration<>(); config.setTypes(Long.class, String.class); config.setWriteThrough(true); try { merger.mergeConfigurations("cache", config); fail("Expected exception as no CacheLoader factory is configured and read-through is enabled."); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("write-through")); } }
@Test public void setReadThroughWithoutLoaderFails() { MutableConfiguration<Long, String> config = new MutableConfiguration<>(); config.setTypes(Long.class, String.class); config.setReadThrough(true); try { merger.mergeConfigurations("cache", config); fail("Expected exception as no CacheLoader factory is configured and read-through is enabled."); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("read-through")); } }
public JCacheConfiguration(Configuration<K, V> configuration) { if (configuration != null) { if (configuration instanceof RedissonConfiguration) { configuration = ((RedissonConfiguration<K, V>)configuration).getJcacheConfig(); } if (configuration instanceof CompleteConfiguration) { delegate = new MutableConfiguration<K, V>((CompleteConfiguration<K, V>) configuration); } else { delegate = new MutableConfiguration<K, V>(); delegate.setStoreByValue(configuration.isStoreByValue()); delegate.setTypes(configuration.getKeyType(), configuration.getValueType()); } } else { delegate = new MutableConfiguration<K, V>(); } this.expiryPolicy = delegate.getExpiryPolicyFactory().create(); }
public JCacheConfiguration(Configuration<K, V> configuration) { if (configuration != null) { if (configuration instanceof RedissonConfiguration) { configuration = ((RedissonConfiguration<K, V>)configuration).getJcacheConfig(); } if (configuration instanceof CompleteConfiguration) { delegate = new MutableConfiguration<K, V>((CompleteConfiguration<K, V>) configuration); } else { delegate = new MutableConfiguration<K, V>(); delegate.setStoreByValue(configuration.isStoreByValue()); delegate.setTypes(configuration.getKeyType(), configuration.getValueType()); } } else { delegate = new MutableConfiguration<K, V>(); } this.expiryPolicy = delegate.getExpiryPolicyFactory().create(); }
@Test public void testCacheUnwrap() { MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); Cache<String, String> cache = cacheManager.createCache("cache", configuration); assertThat(cache.unwrap(org.ehcache.Cache.class), is(instanceOf(org.ehcache.Cache.class))); assertThat(cache.unwrap(cache.getClass()), is(instanceOf(Eh107Cache.class))); cacheManager.close(); }
private MutableConfiguration<Long, String> getConfiguration(final boolean readThrough, final CacheLoader<Long, String> cacheLoader, final boolean writeThrough, final CacheWriter<Long, String> cacheWriter) { MutableConfiguration<Long, String> config = new MutableConfiguration<>(); config.setTypes(Long.class, String.class); config.setReadThrough(readThrough); config.setCacheLoaderFactory(() -> cacheLoader); config.setWriteThrough(writeThrough); config.setCacheWriterFactory(() -> cacheWriter); return config; } }
@Test public void jsr107DefaultEh107IdentityCopierForImmutableTypesWithoutTemplates() { MutableConfiguration<Long, String> stringCacheConfiguration = new MutableConfiguration<>(); stringCacheConfiguration.setTypes(Long.class, String.class); ConfigurationMerger.ConfigHolder<Long, String> configHolder1 = merger.mergeConfigurations("stringCache", stringCacheConfiguration); assertDefaultCopier(configHolder1.cacheConfiguration.getServiceConfigurations()); }
@Test public void testCacheEntryEventUnwrap() { MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); Cache<String, String> cache = cacheManager.createCache("cache", configuration); org.ehcache.event.CacheEvent<String, String> ehEvent = new EhEvent(); Eh107CacheEntryEvent<String, String> cacheEntryEvent = new Eh107CacheEntryEvent.NormalEvent<>(cache, EventType.CREATED, ehEvent, false); assertThat(cacheEntryEvent.unwrap(org.ehcache.event.CacheEvent.class), is(instanceOf(CacheEvent.class))); assertThat(cacheEntryEvent.unwrap(cacheEntryEvent.getClass()), is(instanceOf(Eh107CacheEntryEvent.NormalEvent.class))); }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }