/** See {@link MutableConfiguration#setReadThrough}. */ public void setReadThrough(boolean isReadThrough) { delegate.setReadThrough(isReadThrough); }
/** {@inheritDoc} */ @Override public CacheConfiguration<K, V> setReadThrough(boolean isReadThrough) { super.setReadThrough(isReadThrough); return this; }
@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)); }
@Test public void test107LoaderOverriddenByEhcacheTemplateLoaderWriter() throws Exception { final AtomicBoolean loaderFactoryInvoked = new AtomicBoolean(false); final DumbCacheLoader product2CacheLoader = new DumbCacheLoader(); MutableConfiguration<Long, Product> product2Configuration = new MutableConfiguration<>(); product2Configuration.setTypes(Long.class, Product.class).setReadThrough(true); product2Configuration.setCacheLoaderFactory(() -> { loaderFactoryInvoked.set(true); return product2CacheLoader; }); Cache<Long, Product> productCache2 = cacheManager.createCache("productCache2", product2Configuration); assertThat(loaderFactoryInvoked.get(), is(false)); Product product = productCache2.get(124L); assertThat(product.getId(), is(124L)); assertThat(ProductCacheLoaderWriter.seen, hasItem(124L)); assertThat(product2CacheLoader.seen, is(empty())); CompletionListenerFuture future = new CompletionListenerFuture(); productCache2.loadAll(Collections.singleton(42L), false, future); future.get(); assertThat(ProductCacheLoaderWriter.seen, hasItem(42L)); assertThat(product2CacheLoader.seen, is(empty())); }
@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 jsr107LoaderGetsRegistered() { MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); CacheLoader<Object, Object> mock = mock(CacheLoader.class); RecordingFactory<CacheLoader<Object, Object>> factory = factoryOf(mock); configuration.setReadThrough(true).setCacheLoaderFactory(factory); merger.mergeConfigurations("cache", configuration); assertThat(factory.called, is(true)); verify(cacheLoaderWriterFactory).registerJsr107Loader(eq("cache"), ArgumentMatchers.<CacheLoaderWriter<Object, Object>>isNotNull()); }
@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")); } }
@Test public void jsr107LoaderGetsOverriddenByTemplate() throws Exception { when(jsr107Service.getTemplateNameForCache("cache")).thenReturn("cacheTemplate"); when(xmlConfiguration.newCacheConfigurationBuilderFromTemplate("cacheTemplate", Object.class, Object.class)).thenReturn( newCacheConfigurationBuilder(Object.class, Object.class, heap(10)).add(new DefaultCacheLoaderWriterConfiguration((Class)null)) ); MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); CacheLoader<Object, Object> mock = mock(CacheLoader.class); RecordingFactory<CacheLoader<Object, Object>> factory = factoryOf(mock); configuration.setReadThrough(true).setCacheLoaderFactory(factory); ConfigurationMerger.ConfigHolder<Object, Object> configHolder = merger.mergeConfigurations("cache", configuration); assertThat(factory.called, is(false)); assertThat(configHolder.cacheResources.getCacheLoaderWriter(), nullValue()); }
@Test public void testLoaderConfiguration() throws Exception { final AtomicBoolean loaderCreated = new AtomicBoolean(false); MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class).setReadThrough(true); configuration.setCacheLoaderFactory(() -> { loaderCreated.set(true); return new TestCacheLoader(); }); CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Cache<String, String> cache = cacheManager.createCache("cache", configuration); assertThat(loaderCreated.get(), is(true)); cache.putIfAbsent("42", "The Answer"); TestCacheLoader.seen.clear(); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(Collections.singleton("42"), true, future); future.get(); assertThat(TestCacheLoader.seen, contains("42")); }
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 jsr107ListenerFactoryInitFailureClosesExpiryLoader() throws Exception { ExpiryPolicy expiryPolicy = mock(ExpiryPolicy.class, new MockSettingsImpl<>().extraInterfaces(Closeable.class)); CacheLoader<Object, Object> loader = mock(CacheLoader.class, new MockSettingsImpl<>().extraInterfaces(Closeable.class)); MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); configuration.setExpiryPolicyFactory(factoryOf(expiryPolicy)) .setReadThrough(true) .setCacheLoaderFactory(factoryOf(loader)) .addCacheEntryListenerConfiguration(new ThrowingCacheEntryListenerConfiguration()); try { merger.mergeConfigurations("cache", configuration); fail("Loader factory should have thrown"); } catch (CacheException mce) { verify((Closeable) expiryPolicy).close(); verify((Closeable) loader).close(); } }
@Test public void jsr107LoaderInitFailureClosesExpiry() throws Exception { ExpiryPolicy expiryPolicy = mock(ExpiryPolicy.class, new MockSettingsImpl<>().extraInterfaces(Closeable.class)); MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); Factory<CacheLoader<Object, Object>> factory = throwingFactory(); configuration.setExpiryPolicyFactory(factoryOf(expiryPolicy)) .setReadThrough(true) .setCacheLoaderFactory(factory); try { merger.mergeConfigurations("cache", configuration); fail("Loader factory should have thrown"); } catch (CacheException mce) { verify((Closeable) expiryPolicy).close(); } }
/** {@inheritDoc} */ @Override public CacheConfiguration<K, V> setReadThrough(boolean isReadThrough) { super.setReadThrough(isReadThrough); return this; }
/** See {@link MutableConfiguration#setReadThrough}. */ public void setReadThrough(boolean isReadThrough) { delegate.setReadThrough(isReadThrough); }
@Setup public void setup() { loaderList = targetInstance.getList(name + "loaders"); config = new MutableConfiguration<Integer, Integer>(); config.setReadThrough(true); RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<Integer>(); recordingCacheLoader.loadAllDelayMs = loadAllDelayMs; config.setCacheLoaderFactory(FactoryBuilder.factoryOf(recordingCacheLoader)); CacheManager cacheManager = createCacheManager(targetInstance); cacheManager.createCache(name, config); cache = cacheManager.getCache(name); }
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; } }
/** * Establish the {@link CacheManager} and {@link Cache} for a test. */ @Before public void onBeforeEachTest() throws IOException { //establish and open a CacheLoaderServer to handle cache //cache loading requests from a CacheLoaderClient cacheLoaderServer = new CacheLoaderServer<String, String>(10000); cacheLoaderServer.open(); //establish the CacheManager for the tests cacheManager = Caching.getCachingProvider().getCacheManager(); //establish a CacheLoaderClient that a Cache can use for loading entries //(via the CacheLoaderServer) CacheLoaderClient<String, String> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort()); //establish a Cache Configuration that uses a CacheLoader and Read-Through MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader)); configuration.setReadThrough(true); //configure the cache cacheManager.createCache("cache-loader-test", configuration); cache = cacheManager.getCache("cache-loader-test", String.class, String.class); }
/** * Establish the {@link javax.cache.CacheManager} and {@link javax.cache.Cache} for a test. */ @Before public void onBeforeEachTest() throws IOException { //establish and open a CacheLoaderServer to handle cache //cache loading requests from a CacheLoaderClient cacheLoaderServer = new CacheLoaderServer<String, String>(10000); cacheLoaderServer.open(); //establish the CacheManager for the tests cacheManager = Caching.getCachingProvider().getCacheManager(); //establish a CacheLoaderClient that a Cache can use for loading entries //(via the CacheLoaderServer) CacheLoaderClient<String, String> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort()); //establish a Cache Configuration that uses a CacheLoader (no Read-Through) MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader)); configuration.setReadThrough(false); //configure the cache cacheManager.createCache("cache-loader-test", configuration); cache = cacheManager.getCache("cache-loader-test", String.class, String.class); }
/** * Establish the {@link javax.cache.CacheManager} and {@link javax.cache.Cache} for a test. */ @Before public void onBeforeEachTest() throws IOException { //establish and open a CacheLoaderServer to handle cache //cache loading requests from a CacheLoaderClient cacheLoaderServer = new CacheLoaderServer<String, String>(10000); cacheLoaderServer.open(); //establish the CacheManager for the tests cacheManager = Caching.getCachingProvider().getCacheManager(); //establish a CacheLoaderClient that a Cache can use for loading entries //(via the CacheLoaderServer) CacheLoaderClient<String, String> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort()); //establish a Cache Configuration that uses a CacheLoader, Read-Through and Expiry MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class); configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader)); configuration.setReadThrough(true); configuration.setExpiryPolicyFactory(FactoryBuilder.factoryOf(ExpireOnAccessPolicy.class)); //configure the cache cacheManager.createCache("cache-loader-test", configuration); cache = cacheManager.getCache("cache-loader-test", String.class, String.class); }
@Test public void testCustomConfiguration() throws Exception { boolean storeByValue = false; MutableConfiguration configuration = new MutableConfiguration() .setReadThrough(false).setWriteThrough(false).setStoreByValue(storeByValue) .setTypes(java.math.BigDecimal.class, java.awt.Color.class) .setManagementEnabled(true).setStatisticsEnabled(false); Cache cache = null; try { cache = getCacheManager().createCache("customCache", configuration); } catch (UnsupportedOperationException e) { storeByValue = true; configuration.setStoreByValue(storeByValue); cache = getCacheManager().createCache("customCache", configuration); } assertEquals("java.math.BigDecimal", lookupManagementAttribute(cache, CacheConfiguration, "KeyType")); assertEquals("java.awt.Color", lookupManagementAttribute(cache, CacheConfiguration, "ValueType")); assertEquals(false, lookupManagementAttribute(cache, CacheConfiguration, "ReadThrough")); assertEquals(false, lookupManagementAttribute(cache, CacheConfiguration, "WriteThrough")); assertEquals(storeByValue, lookupManagementAttribute(cache, CacheConfiguration, "StoreByValue")); assertEquals(false, lookupManagementAttribute(cache, CacheConfiguration, "StatisticsEnabled")); assertEquals(true, lookupManagementAttribute(cache, CacheConfiguration, "ManagementEnabled")); //this used to just call close() but that does not work if an implementation maintains statistics on the cluster. cache.getCacheManager().destroyCache("customCache"); }