use of org.infinispan.jcache.util.InMemoryJCacheLoader in project infinispan by infinispan.
the class JCacheLoaderTest method testLoadEntryWithExpiration.
public void testLoadEntryWithExpiration(Method m) {
final String cacheName = m.getName();
withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {
@Override
public void call() {
ControlledTimeService timeService = new ControlledTimeService();
TestingUtil.replaceComponent(cm, TimeService.class, timeService, true);
JCacheManager jCacheManager = createJCacheManager(cm, this);
InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
cacheLoader.store(1, "v1").store(2, "v2");
MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
final long lifespan = 3000;
cfg.setReadThrough(true);
cfg.setExpiryPolicyFactory((Factory<ExpiryPolicy>) () -> new ExpiryPolicy() {
@Override
public Duration getExpiryForCreation() {
return new Duration(TimeUnit.MILLISECONDS, lifespan);
}
@Override
public Duration getExpiryForAccess() {
return null;
}
@Override
public Duration getExpiryForUpdate() {
return Duration.ZERO;
}
});
cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
assertEquals("v2", cache.get(2));
assertEquals("v1", cache.get(1));
timeService.advance(lifespan + 100);
DataContainer<Integer, String> dc = cache.unwrap(AdvancedCache.class).getDataContainer();
assertEquals(null, dc.peek(2));
assertEquals(null, dc.peek(1));
}
});
}
use of org.infinispan.jcache.util.InMemoryJCacheLoader in project infinispan by infinispan.
the class JCacheLoaderTest method testLoadAllWithJCacheLoader.
public void testLoadAllWithJCacheLoader(Method m) {
final String cacheName = m.getName();
// GlobalConfigurationBuilder globalBuilder = new GlobalConfigurationBuilder();
// globalBuilder.asyncTransportExecutor().addProperty("maxThreads", "1");
withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {
@Override
public void call() {
JCacheManager jCacheManager = createJCacheManager(cm, this);
InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
cacheLoader.store(1, "v1").store(2, "v2");
MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
assertEquals(0, cacheLoader.getLoadCount());
CompletionListenerFuture future = new CompletionListenerFuture();
cache.loadAll(Util.asSet(1, 2), true, future);
futureGet(future);
assertEquals(2, cacheLoader.getLoadCount());
}
});
}
use of org.infinispan.jcache.util.InMemoryJCacheLoader in project infinispan by infinispan.
the class JCacheLoaderTest method testLoadUnmarshallableValue.
public void testLoadUnmarshallableValue(Method m) {
final String cacheName = m.getName();
NonMarshallablePojo v1 = new NonMarshallablePojo("v1");
NonMarshallablePojo v2 = new NonMarshallablePojo("v2");
withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {
@Override
public void call() {
JCacheManager jCacheManager = createJCacheManager(cm, this);
InMemoryJCacheLoader<Integer, NonMarshallablePojo> cacheLoader = new InMemoryJCacheLoader<>();
cacheLoader.store(1, v1).store(2, v2);
MutableConfiguration<Integer, NonMarshallablePojo> cfg = new MutableConfiguration<>();
cfg.setStoreByValue(false);
cfg.setReadThrough(true);
cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
Cache<Integer, NonMarshallablePojo> cache = jCacheManager.createCache(cacheName, cfg);
assertEquals(v2, cache.get(2));
assertEquals(v1, cache.get(1));
DataContainer<Integer, String> dc = cache.unwrap(AdvancedCache.class).getDataContainer();
assertEquals(v2, dc.peek(2).getValue());
assertEquals(v1, dc.peek(1).getValue());
}
});
}
Aggregations