/** See {@link MutableConfiguration#setCacheLoaderFactory}. */ public void setCacheLoaderFactory(Factory<? extends CacheLoader<K, V>> factory) { delegate.setCacheLoaderFactory(factory); }
/** {@inheritDoc} */ @Override public CacheConfiguration<K, V> setCacheLoaderFactory(Factory<? extends CacheLoader<K, V>> factory) { super.setCacheLoaderFactory(factory); 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 jsr107LoaderInitAlways() { CacheLoader<Object, Object> loader = mock(CacheLoader.class); MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); RecordingFactory<CacheLoader<Object, Object>> factory = factoryOf(loader); configuration.setCacheLoaderFactory(factory); ConfigurationMerger.ConfigHolder<Object, Object> configHolder = merger.mergeConfigurations("cache", configuration); assertThat(factory.called, is(true)); assertThat(configHolder.cacheResources.getCacheLoaderWriter(), notNullValue()); assertThat(configHolder.useEhcacheLoaderWriter, is(false)); }
@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> setCacheLoaderFactory(Factory<? extends CacheLoader<K, V>> factory) { super.setCacheLoaderFactory(factory); return this; }
/** See {@link MutableConfiguration#setCacheLoaderFactory}. */ public void setCacheLoaderFactory(Factory<? extends CacheLoader<K, V>> factory) { delegate.setCacheLoaderFactory(factory); }
private <T> MutableConfiguration<String, T> configure(final MutableConfiguration<String, T> configuration, final OAuth2Options opts) { ofNullable(opts.getJcacheLoader()) .map(n -> lookup(CacheLoader.class, n)) .ifPresent(l -> configuration.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<CacheLoader<String, T>>(l))); ofNullable(opts.getJcacheWriter()) .map(n -> lookup(CacheWriter.class, n)) .ifPresent(w -> configuration.setCacheWriterFactory(new FactoryBuilder.SingletonFactory<CacheWriter<String, T>>(w))); return configuration .setStoreByValue(opts.isJcacheStoreValue()) .setStatisticsEnabled(opts.isJcacheStatistics()) .setManagementEnabled(opts.isJcacheJmx()); }
@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; } }
@Override public void call() { JCacheManager jCacheManager = createJCacheManager(cm, this); InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<Integer, String>(); cacheLoader.store(1, "v1").store(2, "v2"); MutableConfiguration<Integer, String> cfg = new MutableConfiguration<Integer, String>(); // JDK6 fails to compile when calling FactoryBuilder.factoryOf() :( cfg.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory(cacheLoader)); Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg); assertEquals(0, cacheLoader.getLoadCount()); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(CollectionFactory.makeSet(1, 2), true, future); futureGet(future); assertEquals(2, cacheLoader.getLoadCount()); } });
/** * 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, 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); }