Search in sources :

Example 1 with InMemoryJCacheLoader

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));
        }
    });
}
Also used : JCacheManager(org.infinispan.jcache.embedded.JCacheManager) ControlledTimeService(org.infinispan.util.ControlledTimeService) TimeService(org.infinispan.commons.time.TimeService) Factory(javax.cache.configuration.Factory) TestCacheManagerFactory(org.infinispan.test.fwk.TestCacheManagerFactory) InMemoryJCacheLoader(org.infinispan.jcache.util.InMemoryJCacheLoader) Duration(javax.cache.expiry.Duration) MutableConfiguration(javax.cache.configuration.MutableConfiguration) DataContainer(org.infinispan.container.DataContainer) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) ControlledTimeService(org.infinispan.util.ControlledTimeService) AdvancedCache(org.infinispan.AdvancedCache) Cache(javax.cache.Cache)

Example 2 with InMemoryJCacheLoader

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());
        }
    });
}
Also used : CacheManagerCallable(org.infinispan.test.CacheManagerCallable) JCacheManager(org.infinispan.jcache.embedded.JCacheManager) InMemoryJCacheLoader(org.infinispan.jcache.util.InMemoryJCacheLoader) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AdvancedCache(org.infinispan.AdvancedCache) Cache(javax.cache.Cache)

Example 3 with InMemoryJCacheLoader

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());
        }
    });
}
Also used : DataContainer(org.infinispan.container.DataContainer) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) JCacheManager(org.infinispan.jcache.embedded.JCacheManager) InMemoryJCacheLoader(org.infinispan.jcache.util.InMemoryJCacheLoader) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AdvancedCache(org.infinispan.AdvancedCache) Cache(javax.cache.Cache)

Aggregations

Cache (javax.cache.Cache)3 MutableConfiguration (javax.cache.configuration.MutableConfiguration)3 AdvancedCache (org.infinispan.AdvancedCache)3 JCacheManager (org.infinispan.jcache.embedded.JCacheManager)3 InMemoryJCacheLoader (org.infinispan.jcache.util.InMemoryJCacheLoader)3 CacheManagerCallable (org.infinispan.test.CacheManagerCallable)3 DataContainer (org.infinispan.container.DataContainer)2 Factory (javax.cache.configuration.Factory)1 Duration (javax.cache.expiry.Duration)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 CompletionListenerFuture (javax.cache.integration.CompletionListenerFuture)1 TimeService (org.infinispan.commons.time.TimeService)1 TestCacheManagerFactory (org.infinispan.test.fwk.TestCacheManagerFactory)1 ControlledTimeService (org.infinispan.util.ControlledTimeService)1