本文整理汇总了Java中org.ehcache.spi.loaderwriter.CacheLoaderWriter类的典型用法代码示例。如果您正苦于以下问题:Java CacheLoaderWriter类的具体用法?Java CacheLoaderWriter怎么用?Java CacheLoaderWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CacheLoaderWriter类属于org.ehcache.spi.loaderwriter包,在下文中一共展示了CacheLoaderWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: wrapEhcacheCache
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
private <K, V> Eh107Cache<K, V> wrapEhcacheCache(String alias, InternalCache<K, V> cache) {
CacheLoaderWriter<? super K, V> cacheLoaderWriter = cache.getCacheLoaderWriter();
boolean storeByValueOnHeap = false;
for (ServiceConfiguration<?> serviceConfiguration : cache.getRuntimeConfiguration().getServiceConfigurations()) {
if (serviceConfiguration instanceof DefaultCopierConfiguration) {
DefaultCopierConfiguration<?> copierConfig = (DefaultCopierConfiguration) serviceConfiguration;
if(!copierConfig.getClazz().isAssignableFrom(IdentityCopier.class))
storeByValueOnHeap = true;
break;
}
}
Eh107Configuration<K, V> config = new Eh107ReverseConfiguration<>(cache, cacheLoaderWriter != null, cacheLoaderWriter != null, storeByValueOnHeap);
configurationMerger.setUpManagementAndStats(cache, config);
Eh107Expiry<K, V> expiry = new EhcacheExpiryWrapper<>(cache.getRuntimeConfiguration().getExpiryPolicy());
CacheResources<K, V> resources = new CacheResources<>(alias, wrapCacheLoaderWriter(cacheLoaderWriter), expiry);
return new Eh107Cache<>(alias, config, resources, cache, this);
}
示例2: BatchingLocalHeapWriteBehindQueue
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
public BatchingLocalHeapWriteBehindQueue(ExecutionService executionService, String defaultThreadPool, WriteBehindConfiguration config, CacheLoaderWriter<K, V> cacheLoaderWriter) {
super(cacheLoaderWriter);
this.cacheLoaderWriter = cacheLoaderWriter;
BatchingConfiguration batchingConfig = config.getBatchingConfiguration();
this.maxWriteDelayMs = batchingConfig.getMaxDelayUnit().toMillis(batchingConfig.getMaxDelay());
this.batchSize = batchingConfig.getBatchSize();
this.coalescing = batchingConfig.isCoalescing();
this.executorQueue = new LinkedBlockingQueue<>(config.getMaxQueueSize() / batchSize);
if (config.getThreadPoolAlias() == null) {
this.executor = executionService.getOrderedExecutor(defaultThreadPool, executorQueue);
} else {
this.executor = executionService.getOrderedExecutor(config.getThreadPoolAlias(), executorQueue);
}
if (config.getThreadPoolAlias() == null) {
this.scheduledExecutor = executionService.getScheduledExecutor(defaultThreadPool);
} else {
this.scheduledExecutor = executionService.getScheduledExecutor(config.getThreadPoolAlias());
}
}
示例3: testBatchedDeletedKeyReturnsNull
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testBatchedDeletedKeyReturnsNull() throws Exception {
@SuppressWarnings("unchecked")
CacheLoaderWriter<String, String> loaderWriter = mock(CacheLoaderWriter.class);
when(loaderWriter.load("key")).thenReturn("value");
CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter);
try (CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true)) {
Cache<String, String> testCache = cacheManager.createCache("testBatchedDeletedKeyReturnsNull", configurationBuilder()
.add(newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 2).build())
.build());
assertThat(testCache.get("key"), is("value"));
testCache.remove("key");
assertThat(testCache.get("key"), nullValue());
}
}
示例4: testBatchedOverwrittenKeyReturnsNewValue
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testBatchedOverwrittenKeyReturnsNewValue() throws Exception {
@SuppressWarnings("unchecked")
CacheLoaderWriter<String, String> loaderWriter = mock(CacheLoaderWriter.class);
when(loaderWriter.load("key")).thenReturn("value");
CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter);
try (CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true)) {
Cache<String, String> testCache = cacheManager.createCache("testBatchedOverwrittenKeyReturnsNewValue", configurationBuilder()
.add(newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 2).build())
.build());
assertThat(testCache.get("key"), is("value"));
testCache.put("key", "value2");
assertThat(testCache.get("key"), is("value2"));
}
}
示例5: testAddingWriteBehindConfigurationAtCacheLevel
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testAddingWriteBehindConfigurationAtCacheLevel() {
CacheManagerBuilder<CacheManager> cacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder();
WriteBehindConfiguration writeBehindConfiguration = WriteBehindConfigurationBuilder.newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 1)
.concurrencyLevel(3)
.queueSize(10)
.build();
Class<CacheLoaderWriter<?, ?>> klazz = (Class<CacheLoaderWriter<?, ?>>) (Class) (SampleLoaderWriter.class);
CacheManager cacheManager = cacheManagerBuilder.build(true);
final Cache<Long, String> cache = cacheManager.createCache("cache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(100))
.add(writeBehindConfiguration)
.add(new DefaultCacheLoaderWriterConfiguration(klazz))
.build());
Collection<ServiceConfiguration<?>> serviceConfiguration = cache.getRuntimeConfiguration()
.getServiceConfigurations();
assertThat(serviceConfiguration, IsCollectionContaining.<ServiceConfiguration<?>>hasItem(instanceOf(WriteBehindConfiguration.class)));
cacheManager.close();
}
示例6: testPutAllWithWriter
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testPutAllWithWriter() throws Exception {
Store<Number, CharSequence> store = mock(Store.class);
when(store.bulkCompute((Set<Integer>) argThat(hasItems(1, 2, 3)), any(Function.class))).thenAnswer(invocation -> {
Function<List<Map.Entry<Integer, String>>, Object> function =
(Function<List<Map.Entry<Integer, String>>, Object>)invocation.getArguments()[1];
function.apply(Arrays.asList(entry(1, "one"), entry(2, "two"), entry(3, "three")));
return null;
});
CacheLoaderWriter<Number, CharSequence> cacheLoaderWriter = mock(CacheLoaderWriter.class);
InternalCache<Number, CharSequence> ehcache = getCache(store, cacheLoaderWriter);
ehcache.init();
ehcache.putAll(new LinkedHashMap<Number, CharSequence>() {{
put(1, "one");
put(2, "two");
put(3, "three");
}});
verify(store).bulkCompute((Set<? extends Number>) argThat(hasItems(1, 2, 3)), any(Function.class));
verify(cacheLoaderWriter).writeAll(argThat(hasItems(entry(1, "one"), entry(2, "two"), entry(3, "three"))));
}
示例7: testRemoveAllWithWriter
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testRemoveAllWithWriter() throws Exception {
Store<Number, CharSequence> store = mock(Store.class);
when(store.bulkCompute((Set<? extends Number>) argThat(hasItems(1, 2, 3)), any(Function.class))).thenAnswer(invocation -> {
Function function = (Function)invocation.getArguments()[1];
function.apply(Arrays.asList(entry(1, "one"), entry(2, "two"), entry(3, "three")));
return null;
});
CacheLoaderWriter<Number, CharSequence> cacheLoaderWriter = mock(CacheLoaderWriter.class);
InternalCache<Number, CharSequence> ehcache = getCache(store, cacheLoaderWriter);
ehcache.init();
ehcache.removeAll(new LinkedHashSet<Number>(Arrays.asList(1, 2, 3)));
verify(store).bulkCompute((Set<? extends Number>) argThat(hasItems(1, 2, 3)), any(Function.class));
verify(cacheLoaderWriter).deleteAll(argThat(hasItems(1, 2, 3)));
}
示例8: getOrCreateCache
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
/**
* Gets the named ehCache instance or builds it if it doesn't exist. Cache configuration comes from the StroomPropertyService
* and the passed optional CacheLoaderWriter. If you need a more customised cache then use CacheConfigurationService to get
* a cacheConfigurationBuilder to further customise.
*/
@Override
public <K, V> Cache<K, V> getOrCreateCache(final String cacheName,
final Class<K> keyType,
final Class<V> valueType,
final Optional<CacheLoaderWriter<K, V>> optLoaderWriter) {
Cache<K, V> cache = cacheManager.getCache(cacheName, keyType, valueType);
if (cache == null) {
//cacheManger is a singleton so all threads can synch on that
synchronized (cacheManager) {
cache = cacheManager.getCache(cacheName, keyType, valueType);
if (cache == null) {
//construct the cache
try {
CacheConfigurationBuilder<K, V> builder = cacheConfigurationService.newCacheConfigurationBuilder(
cacheName, keyType, valueType);
if (optLoaderWriter.isPresent()) {
//withLoaderWriter returns a new instance of builder
builder = builder.withLoaderWriter(optLoaderWriter.get());
}
cache = cacheManager.createCache(cacheName, builder.build());
} catch (Exception e) {
throw new RuntimeException(String.format(
"Error trying to create cache %s: %s. Is all the cache configuration correct in Zookeeper?",
cacheName,
e.getMessage()), e);
}
}
}
}
return cache;
}
示例9: setUp
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
cacheLoaderWriter = mock(CacheLoaderWriter.class);
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), (CacheConfiguration<Number, CharSequence>) any())).thenReturn((CacheLoaderWriter) cacheLoaderWriter);
cacheManager = newCacheManagerBuilder().using(cacheLoaderWriterProvider).build(true);
testCache = cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)).build());
}
示例10: testPutAll_with_cache_writer
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testPutAll_with_cache_writer() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder
.build();
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriter).write(ArgumentMatchers
.any(), ArgumentMatchers.any());
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
HashMap<String, String> stringStringHashMap = new HashMap<>();
for (int i = 0; i < 3; i++) {
stringStringHashMap.put("key" + i, "value" + i);
}
// the call to putAll
myCache.putAll(stringStringHashMap);
verify(cacheLoaderWriter, times(3)).writeAll(ArgumentMatchers.any(Iterable.class));
Set set = new HashSet(){{add(entry("key0", "value0"));}};
verify(cacheLoaderWriter).writeAll(set);
set = new HashSet(){{add(entry("key1", "value1"));}};
verify(cacheLoaderWriter).writeAll(set);
set = new HashSet(){{add(entry("key2", "value2"));}};
verify(cacheLoaderWriter).writeAll(set);
for (int i = 0; i < 3; i++) {
assertThat(myCache.get("key" + i), is("value" + i));
}
}
示例11: testPutAll_with_cache_writer_that_throws_exception
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testPutAll_with_cache_writer_that_throws_exception() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder
.build();
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
CacheLoaderWriter cacheLoaderWriterThatThrows = mock(CacheLoaderWriter.class);
doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriterThatThrows).write(ArgumentMatchers
.any(), ArgumentMatchers.any());
doThrow(new Exception("Simulating an exception from the cache writer")).when(cacheLoaderWriterThatThrows).writeAll(ArgumentMatchers.any(Iterable.class));
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriterThatThrows);
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
HashMap<String, String> stringStringHashMap = new HashMap<>();
for (int i = 0; i < 3; i++) {
stringStringHashMap.put("key" + i, "value" + i);
}
// the call to putAll
try {
myCache.putAll(stringStringHashMap);
fail();
} catch (BulkCacheWritingException bcwe) {
assertThat(bcwe.getFailures().size(), is(3));
assertThat(bcwe.getSuccesses().size(), is(0));
}
}
示例12: testPutAll_store_throws_cache_exception
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testPutAll_store_throws_cache_exception() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder
.build();
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriter).write(ArgumentMatchers.any(), ArgumentMatchers.any());
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider).using(new CustomStoreProvider());
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
Map<String, String> stringStringHashMap = new HashMap<>();
for (int i = 0; i < 3; i++) {
stringStringHashMap.put("key" + i, "value" + i);
}
// the call to putAll
myCache.putAll(stringStringHashMap);
for (int i = 0; i < 3; i++) {
// the store threw an exception when we call bulkCompute
assertThat(myCache.get("key" + i), is(nullValue()));
// but still, the cache writer could writeAll the values !
// assertThat(cacheWriterToHashMapMap.get("key" + i), is("value" + i));
}
// but still, the cache writer could writeAll the values at once !
verify(cacheLoaderWriter, times(1)).writeAll(ArgumentMatchers.any(Iterable.class));
Set set = new HashSet() {{add(entry("key0", "value0")); add(entry("key1", "value1")); add(entry("key2", "value2"));}};
verify(cacheLoaderWriter).writeAll(set);
}
示例13: testGetAll_with_cache_loader
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testGetAll_with_cache_loader() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build();
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
when(cacheLoaderWriter.loadAll(argThat(IsCollectionContaining.<String>hasItem("key0")))).thenReturn(new HashMap(){{put("key0","value0");}});
when(cacheLoaderWriter.loadAll(argThat(IsCollectionContaining.<String>hasItem("key2")))).thenReturn(new HashMap(){{put("key2","value2");}});
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
Set<String> fewKeysSet = new HashSet<String>() {
{
add("key0");
add("key2");
}
};
// the call to getAll
Map<String, String> fewEntries = myCache.getAll(fewKeysSet);
assertThat(fewEntries.size(), is(2));
assertThat(fewEntries.get("key0"), is("value0"));
assertThat(fewEntries.get("key2"), is("value2"));
}
示例14: testGetAll_cache_loader_throws_exception
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testGetAll_cache_loader_throws_exception() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build();
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
when(cacheLoaderWriter.loadAll(ArgumentMatchers.any(Iterable.class))).thenThrow(new Exception("Simulating an exception from the cache loader"));
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
Set<String> fewKeysSet = new HashSet<String>() {
{
add("key0");
add("key2");
}
};
// the call to getAll
try {
myCache.getAll(fewKeysSet);
fail();
} catch (BulkCacheLoadingException bcwe) {
// since onHeapStore.bulkComputeIfAbsent sends batches of 1 element,
assertThat(bcwe.getFailures().size(), is(2));
assertThat(bcwe.getSuccesses().size(), is(0));
}
}
示例15: testGetAll_store_throws_cache_exception
import org.ehcache.spi.loaderwriter.CacheLoaderWriter; //导入依赖的package包/类
@Test
public void testGetAll_store_throws_cache_exception() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build();
CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider).using(new CustomStoreProvider());
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
when(cacheLoaderWriter.loadAll(argThat(hasItems("key0", "key2")))).thenReturn( new HashMap(){{put("key0","value0"); put("key2","value2");}});
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
Set<String> fewKeysSet = new HashSet<String>() {
{
add("key0");
add("key2");
}
};
// the call to getAll
Map<String, String> fewEntries = myCache.getAll(fewKeysSet);
assertThat(fewEntries.size(), is(2));
assertThat(fewEntries.get("key0"), is("value0"));
assertThat(fewEntries.get("key2"), is("value2"));
}