javax.cache.integration.CacheLoader Java Examples
The following examples show how to use
javax.cache.integration.CacheLoader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigurationMergerTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@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(); } }
Example #2
Source File: TCacheJSR107.java From triava with Apache License 2.0 | 6 votes |
private TCacheJSR107MutableEntry<K, V> invokeBuildMutableEntry(K key) { AccessTimeObjectHolder<V> holder = tcache.peekHolder(key); V value = holder != null ? holder.peek() : null; // Create surrogate "null" if not existing (JSR107) // Statistics effects are not properly described in the Spec. See https://github.com/jsr107/RI/issues/54 // Thus I am following here what the RI does. if (value != null) tcache.statisticsCalculator.incrementHitCount(); else tcache.statisticsCalculator.incrementMissCount(); CacheLoader<K, V> loader = tcache.builder.isReadThrough() ? tcache.loader : null; TCacheJSR107MutableEntry<K, V> me = new TCacheJSR107MutableEntry<K, V>(key, value, loader); return me; }
Example #3
Source File: CompositeCacheLoader.java From commons-jcs with Apache License 2.0 | 6 votes |
@Override public Map<K, V> loadAll(final Iterable<? extends K> keys) throws CacheLoaderException { final Collection<K> list = new ArrayList<K>(); for (final K k : keys) { list.add(k); } final Map<K, V> result = new HashMap<K, V>(); for (final CacheLoader<K, V> delegate : delegates) { final Map<K, V> v = delegate.loadAll(list); if (v != null) { result.putAll(v); list.removeAll(v.keySet()); if (list.isEmpty()) { return v; } } } return result; }
Example #4
Source File: ConfigurationMergerTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@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(); } }
Example #5
Source File: ConfigurationMergerTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@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)).withService(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()); }
Example #6
Source File: ConfigurationMerger.java From ehcache3 with Apache License 2.0 | 6 votes |
private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfiguration<K, V> config) { Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory(); @SuppressWarnings("unchecked") Factory<CacheWriter<K, V>> cacheWriterFactory = (Factory<CacheWriter<K, V>>) (Object) config.getCacheWriterFactory(); if (config.isReadThrough() && cacheLoaderFactory == null) { throw new IllegalArgumentException("read-through enabled without a CacheLoader factory provided"); } if (config.isWriteThrough() && cacheWriterFactory == null) { throw new IllegalArgumentException("write-through enabled without a CacheWriter factory provided"); } CacheLoader<K, V> cacheLoader = cacheLoaderFactory == null ? null : cacheLoaderFactory.create(); CacheWriter<K, V> cacheWriter; try { cacheWriter = cacheWriterFactory == null ? null : cacheWriterFactory.create(); } catch (Throwable t) { throw closeAllAfter(new CacheException(t), cacheLoader); } if (cacheLoader == null && cacheWriter == null) { return null; } else { return new Eh107CacheLoaderWriter<>(cacheLoader, config.isReadThrough(), cacheWriter, config.isWriteThrough()); } }
Example #7
Source File: CacheProxy.java From caffeine with Apache License 2.0 | 6 votes |
@SuppressWarnings({"PMD.ExcessiveParameterList", "NullAway"}) public CacheProxy(String name, Executor executor, CacheManager cacheManager, CaffeineConfiguration<K, V> configuration, com.github.benmanes.caffeine.cache.Cache<K, Expirable<V>> cache, EventDispatcher<K, V> dispatcher, Optional<CacheLoader<K, V>> cacheLoader, ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics) { this.configuration = requireNonNull(configuration); this.cacheManager = requireNonNull(cacheManager); this.cacheLoader = requireNonNull(cacheLoader); this.dispatcher = requireNonNull(dispatcher); this.statistics = requireNonNull(statistics); this.executor = requireNonNull(executor); this.expiry = requireNonNull(expiry); this.ticker = requireNonNull(ticker); this.cache = requireNonNull(cache); this.name = requireNonNull(name); copier = configuration.isStoreByValue() ? configuration.getCopierFactory().create() : Copier.identity(); writer = configuration.hasCacheWriter() ? configuration.getCacheWriter() : DisabledCacheWriter.get(); cacheMXBean = new JCacheMXBean(this); }
Example #8
Source File: CacheFactory.java From caffeine with Apache License 2.0 | 5 votes |
/** Creates a cache that reads through on a cache miss. */ private CacheProxy<K, V> newLoadingCacheProxy() { CacheLoader<K, V> cacheLoader = config.getCacheLoaderFactory().create(); JCacheLoaderAdapter<K, V> adapter = new JCacheLoaderAdapter<>( cacheLoader, dispatcher, expiryPolicy, ticker, statistics); CacheProxy<K, V> cache = new LoadingCacheProxy<>(cacheName, executor, cacheManager, config, caffeine.build(adapter), dispatcher, cacheLoader, expiryPolicy, ticker, statistics); adapter.setCache(cache); return cache; }
Example #9
Source File: ConfigurationMergerTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@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)); }
Example #10
Source File: LoaderWriterConfigTest.java From ehcache3 with Apache License 2.0 | 5 votes |
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; }
Example #11
Source File: JCacheLoaderAdapter.java From caffeine with Apache License 2.0 | 5 votes |
@SuppressWarnings("NullAway.Init") public JCacheLoaderAdapter(CacheLoader<K, V> delegate, EventDispatcher<K, V> dispatcher, ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics) { this.dispatcher = requireNonNull(dispatcher); this.statistics = requireNonNull(statistics); this.delegate = requireNonNull(delegate); this.expiry = requireNonNull(expiry); this.ticker = requireNonNull(ticker); }
Example #12
Source File: CacheFactory.java From caffeine with Apache License 2.0 | 5 votes |
/** Creates a cache that does not read through on a cache miss. */ private CacheProxy<K, V> newCacheProxy() { Optional<CacheLoader<K, V>> cacheLoader = Optional.ofNullable(config.getCacheLoaderFactory()).map(Factory::create); return new CacheProxy<>(cacheName, executor, cacheManager, config, caffeine.build(), dispatcher, cacheLoader, expiryPolicy, ticker, statistics); }
Example #13
Source File: JCacheConfigurer.java From openwebbeans-meecrowave with Apache License 2.0 | 5 votes |
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()); }
Example #14
Source File: EntryProcessorEntry.java From caffeine with Apache License 2.0 | 5 votes |
public EntryProcessorEntry(K key, @Nullable V value, Optional<CacheLoader<K, V>> cacheLoader) { this.hasEntry = (value != null); this.cacheLoader = cacheLoader; this.action = Action.NONE; this.value = value; this.key = key; }
Example #15
Source File: LoadingCacheProxy.java From caffeine with Apache License 2.0 | 5 votes |
@SuppressWarnings("PMD.ExcessiveParameterList") public LoadingCacheProxy(String name, Executor executor, CacheManager cacheManager, CaffeineConfiguration<K, V> configuration, LoadingCache<K, Expirable<V>> cache, EventDispatcher<K, V> dispatcher, CacheLoader<K, V> cacheLoader, ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics) { super(name, executor, cacheManager, configuration, cache, dispatcher, Optional.of(cacheLoader), expiry, ticker, statistics); this.cache = cache; }
Example #16
Source File: CacheLoaderTest.java From caffeine with Apache License 2.0 | 5 votes |
@Override protected CacheLoader<Integer, Integer> getCacheLoader() { return new CacheLoader<Integer, Integer>() { @Override public Integer load(Integer key) { return loadSupplier.get(); } @Override public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys) { return loadAllSupplier.get(); } }; }
Example #17
Source File: AbstractJCacheTest.java From caffeine with Apache License 2.0 | 5 votes |
/** The cache loader used by the test. */ protected CacheLoader<Integer, Integer> getCacheLoader() { return new CacheLoader<Integer, Integer>() { @Override public Integer load(Integer key) { return key; } @Override public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys) { return Maps.asMap(ImmutableSet.copyOf(keys), this::load); } }; }
Example #18
Source File: OpenJPAJCacheDataCacheManager.java From commons-jcs with Apache License 2.0 | 5 votes |
Cache<Object, Object> getOrCreateCache(final String prefix, final String entity) { final String internalName = prefix + entity; Cache<Object, Object> cache = cacheManager.getCache(internalName); if (cache == null) { final Properties properties = cacheManager.getProperties(); final MutableConfiguration<Object, Object> configuration = new MutableConfiguration<Object, Object>() .setStoreByValue("true".equalsIgnoreCase(properties.getProperty("jcache.store-by-value", "false"))); configuration.setReadThrough("true".equals(properties.getProperty("jcache.read-through", "false"))); configuration.setWriteThrough("true".equals(properties.getProperty("jcache.write-through", "false"))); if (configuration.isReadThrough()) { configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<Object, Object>>(properties.getProperty("jcache.cache-loader-factory"))); } if (configuration.isWriteThrough()) { configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<Object, Object>>(properties.getProperty("jcache.cache-writer-factory"))); } final String expirtyPolicy = properties.getProperty("jcache.expiry-policy-factory"); if (expirtyPolicy != null) { configuration.setExpiryPolicyFactory(new FactoryBuilder.ClassFactory<ExpiryPolicy>(expirtyPolicy)); } else { configuration.setExpiryPolicyFactory(new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.FIVE_MINUTES))); } configuration.setManagementEnabled("true".equals(properties.getProperty("jcache.management-enabled", "false"))); configuration.setStatisticsEnabled("true".equals(properties.getProperty("jcache.statistics-enabled", "false"))); cache = cacheManager.createCache(internalName, configuration); } return cache; }
Example #19
Source File: CompositeCacheLoader.java From commons-jcs with Apache License 2.0 | 5 votes |
@Override public V load(final K key) throws CacheLoaderException { for (final CacheLoader<K, V> delegate : delegates) { final V v = delegate.load(key); if (v != null) { return v; } } return null; }
Example #20
Source File: CacheLoaderServer.java From cache2k with Apache License 2.0 | 5 votes |
/** * Constructs an CacheLoaderServer. * * @param port the port on which to accept {@link CacheLoaderClient} requests * @param cacheLoader (optional) the {@link CacheLoader} that will be used to handle * client requests */ public CacheLoaderServer(int port, CacheLoader<K, V> cacheLoader) { super(port); // establish the client-server operation handlers addOperationHandler(new LoadOperationHandler()); addOperationHandler(new LoadAllOperationHandler()); this.cacheLoader = cacheLoader; }
Example #21
Source File: Eh107CacheLoaderWriter.java From ehcache3 with Apache License 2.0 | 5 votes |
Eh107CacheLoaderWriter(CacheLoader<K, V> cacheLoader, boolean readThrough, CacheWriter<K, V> cacheWriter, boolean writeThrough) { this.cacheLoader = cacheLoader; this.readThrough = cacheLoader != null && readThrough; if (writeThrough) { this.cacheWriter = cacheWriter; } else { this.cacheWriter = null; } }
Example #22
Source File: ConfigurationMergerTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@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()); }
Example #23
Source File: GridCacheLoaderWriterStore.java From ignite with Apache License 2.0 | 5 votes |
/** * @param ldr Loader. * @param writer Writer. */ GridCacheLoaderWriterStore(@Nullable CacheLoader<K, V> ldr, @Nullable CacheWriter<K, V> writer) { assert ldr != null || writer != null; this.ldr = ldr; this.writer = writer; }
Example #24
Source File: GridCacheLoaderWriterStoreFactory.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public CacheStore<K, V> create() { CacheLoader<K, V> ldr = ldrFactory == null ? null : ldrFactory.create(); CacheWriter<K, V> writer = writerFactory == null ? null : writerFactory.create(); return new GridCacheLoaderWriterStore<>(ldr, writer); }
Example #25
Source File: GridCacheLoaderWriterStoreFactory.java From ignite with Apache License 2.0 | 5 votes |
/** * @param ldrFactory Loader factory. * @param writerFactory Writer factory. */ GridCacheLoaderWriterStoreFactory(@Nullable Factory<CacheLoader<K, V>> ldrFactory, @Nullable Factory<CacheWriter<K, V>> writerFactory) { this.ldrFactory = ldrFactory; this.writerFactory = writerFactory; assert ldrFactory != null || writerFactory != null; }
Example #26
Source File: CopyCacheProxy.java From cache2k with Apache License 2.0 | 4 votes |
/** * Delegates to the wrapped cache. Wrap configuration and return true on store by value */ @SuppressWarnings("unchecked") @Override public <C extends Configuration<K, T>> C getConfiguration(Class<C> clazz) { final C c = cache.getConfiguration(clazz); if (c instanceof CompleteConfiguration) { final CompleteConfiguration<K, T> cc = (CompleteConfiguration<K,T>) c; return (C) new CompleteConfiguration<K, T>() { @Override public Iterable<CacheEntryListenerConfiguration<K, T>> getCacheEntryListenerConfigurations() { return cc.getCacheEntryListenerConfigurations(); } @Override public boolean isReadThrough() { return cc.isReadThrough(); } @Override public boolean isWriteThrough() { return cc.isWriteThrough(); } @Override public boolean isStatisticsEnabled() { return cc.isStatisticsEnabled(); } @Override public boolean isManagementEnabled() { return cc.isManagementEnabled(); } @Override public Factory<CacheLoader<K, T>> getCacheLoaderFactory() { return cc.getCacheLoaderFactory(); } @Override public Factory<CacheWriter<? super K, ? super T>> getCacheWriterFactory() { return cc.getCacheWriterFactory(); } @Override public Factory<ExpiryPolicy> getExpiryPolicyFactory() { return cc.getExpiryPolicyFactory(); } @Override public Class<K> getKeyType() { return cc.getKeyType(); } @Override public Class<T> getValueType() { return cc.getValueType(); } @Override public boolean isStoreByValue() { return true; } }; } else if (c instanceof Configuration) { return (C) new Configuration<K, T>() { @Override public Class<K> getKeyType() { return c.getKeyType(); } @Override public Class<T> getValueType() { return c.getValueType(); } @Override public boolean isStoreByValue() { return true; } }; } return c; }
Example #27
Source File: CacheLoaderServer.java From cache2k with Apache License 2.0 | 4 votes |
public CacheLoader<K, V> getCacheLoader() { return cacheLoader; }
Example #28
Source File: RepositoryConfiguration.java From mobi with GNU Affero General Public License v3.0 | 4 votes |
@Override public Factory<CacheLoader<K, V>> getCacheLoaderFactory() { return delegate.getCacheLoaderFactory(); }
Example #29
Source File: CacheTest.java From commons-jcs with Apache License 2.0 | 4 votes |
@Test public void loader() { final CachingProvider cachingProvider = Caching.getCachingProvider(); final CacheManager cacheManager = cachingProvider.getCacheManager(); cacheManager.createCache("default", new CompleteConfiguration<Object, Object>() { /** * */ private static final long serialVersionUID = -4598329777808827966L; @Override public boolean isReadThrough() { return true; } @Override public boolean isWriteThrough() { return false; } @Override public boolean isStatisticsEnabled() { return false; } @Override public boolean isManagementEnabled() { return false; } @Override public Iterable<CacheEntryListenerConfiguration<Object, Object>> getCacheEntryListenerConfigurations() { return null; } @Override public Factory<CacheLoader<Object, Object>> getCacheLoaderFactory() { return () -> new CacheLoader<Object, Object>() { @Override public Object load(Object key) throws CacheLoaderException { return "super"; } @Override public Map<Object, Object> loadAll(Iterable<?> keys) throws CacheLoaderException { return null; } }; } @Override public Factory<CacheWriter<? super Object, ? super Object>> getCacheWriterFactory() { return null; } @Override public Factory<ExpiryPolicy> getExpiryPolicyFactory() { return null; } @Override public Class<Object> getKeyType() { return Object.class; } @Override public Class<Object> getValueType() { return Object.class; } @Override public boolean isStoreByValue() { return false; } }); final Cache<String, String> cache = cacheManager.getCache("default"); assertEquals("super", cache.get("lazilyLoaded")); cachingProvider.close(); }
Example #30
Source File: JCSConfiguration.java From commons-jcs with Apache License 2.0 | 4 votes |
@Override public Factory<CacheLoader<K, V>> getCacheLoaderFactory() { return cacheLoaderFactory; }