Java Code Examples for javax.cache.configuration.MutableConfiguration#setCacheLoaderFactory()

The following examples show how to use javax.cache.configuration.MutableConfiguration#setCacheLoaderFactory() . 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: SimpleEh107ConfigTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example 2
Source File: CacheLoaderWithoutReadThroughTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * 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 (no Read-Through)
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
  configuration.setReadThrough(false);

  //configure the cache
  cacheManager.createCache("cache-loader-test", configuration);
  cache = cacheManager.getCache("cache-loader-test", String.class, String.class);
}
 
Example 3
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeAllReadThroughEnabledGetOnNonExistentEntry() throws IOException {
    //establish and open a CacheLoaderServer to handle cache
    //cache loading requests from a CacheLoaderClient

    // this cacheLoader just returns the key as the value.
    RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicy));
    config.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(recordingCacheLoader));
    config.setReadThrough(true);

    Cache<Integer, Integer> cache = getCacheManager().createCache("test-1", config);

    final Integer INITIAL_KEY = 123;
    final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;

    // set keys to read through
    Set<Integer> keys = new HashSet<>();
    for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
        keys.add(key);
    }

    // verify read-through of getValue of non-existent entries
    cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

    assertTrue(expiryPolicy.getCreationCount() >= keys.size());
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

}
 
Example 5
Source File: Eh107XmlIntegrationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example 6
Source File: ConfigurationMergerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: LoaderWriterConfigTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
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 8
Source File: OpenJPAJCacheDataCacheManager.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
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 9
Source File: CacheLoaderWriterTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Establish the {@link javax.cache.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
  recordingCacheLoader = new RecordingCacheLoader<String>();
  cacheLoaderServer = new CacheLoaderServer<String, String>(10000, recordingCacheLoader);
  cacheLoaderServer.open();

  // establish and open a CacheWriterServer to handle cache
  // cache loading requests from a CacheWriterClient
  recordingCacheWriter = new RecordingCacheWriter<>();
  cacheWriterServer = new CacheWriterServer<>(10001, recordingCacheWriter);
  cacheWriterServer.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 CacheWriterClient that a Cache can use for writing/deleting entries
  // (via the CacheWriterServer)
  CacheWriterClient<String, String> cacheWriter = new CacheWriterClient<>(cacheWriterServer.getInetAddress(),
      cacheWriterServer.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);
  configuration.setCacheWriterFactory(FactoryBuilder.factoryOf(cacheWriter));
  configuration.setWriteThrough(true);

  //configure the cache
  cacheManager.createCache("cache-loader-writer-test", configuration);
  cache = cacheManager.getCache("cache-loader-writer-test", String.class, String.class);
}
 
Example 10
Source File: CacheLoaderWithExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry() throws IOException {

  //establish and open a CacheLoaderServer to handle cache
  //cache loading requests from a CacheLoaderClient

  // this cacheLoader just returns the key as the value.
  RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
  try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
    cacheLoaderServer.open();

    //establish a CacheLoaderClient that a Cache can use for loading entries
    //(via the CacheLoaderServer)
    CacheLoaderClient<Integer, Integer> cacheLoader =
        new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    expiryPolicyServer.setExpiryPolicy(expiryPolicy);

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
    config.setReadThrough(true);
    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

    final Integer key = 123;
    final Integer recordingCacheLoaderValue = key;

    // verify create when read through is enabled and entry was non-existent in cache.
    Integer resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());

    assertEquals(recordingCacheLoaderValue, resultValue);
    assertTrue(recordingCacheLoader.hasLoaded(key));

    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    closeTestCache();
  }
}
 
Example 12
Source File: JCacheFilter.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public void init(final FilterConfig filterConfig) throws ServletException
{
    final ClassLoader classLoader = filterConfig.getServletContext().getClassLoader();
    provider = Caching.getCachingProvider(classLoader);

    String uri = filterConfig.getInitParameter("configuration");
    if (uri == null)
    {
        uri = provider.getDefaultURI().toString();
    }
    final Properties properties = new Properties();
    for (final String key : list(filterConfig.getInitParameterNames()))
    {
        final String value = filterConfig.getInitParameter(key);
        if (value != null)
        {
            properties.put(key, value);
        }
    }
    manager = provider.getCacheManager(URI.create(uri), classLoader, properties);

    String cacheName = filterConfig.getInitParameter("cache-name");
    if (cacheName == null)
    {
        cacheName = JCacheFilter.class.getName();
    }
    cache = manager.getCache(cacheName);
    if (cache == null)
    {
        final MutableConfiguration<PageKey, Page> configuration = new MutableConfiguration<PageKey, Page>()
                .setStoreByValue(false);
        configuration.setReadThrough("true".equals(properties.getProperty("read-through", "false")));
        configuration.setWriteThrough("true".equals(properties.getProperty("write-through", "false")));
        if (configuration.isReadThrough())
        {
            configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<PageKey, Page>>(properties.getProperty("cache-loader-factory")));
        }
        if (configuration.isWriteThrough())
        {
            configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<? super PageKey, ? super Page>>(properties.getProperty("cache-writer-factory")));
        }
        final String expirtyPolicy = properties.getProperty("expiry-policy-factory");
        if (expirtyPolicy != null)
        {
            configuration.setExpiryPolicyFactory(new FactoryBuilder.ClassFactory<ExpiryPolicy>(expirtyPolicy));
        }
        configuration.setManagementEnabled("true".equals(properties.getProperty("management-enabled", "false")));
        configuration.setStatisticsEnabled("true".equals(properties.getProperty("statistics-enabled", "false")));
        cache = manager.createCache(cacheName, configuration);
    }
}
 
Example 13
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test
public void invokeAllReadThroughEnabledGetOnNonExistentEntry() throws IOException {
  //establish and open a CacheLoaderServer to handle cache
  //cache loading requests from a CacheLoaderClient

  // this cacheLoader just returns the key as the value.
  RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
  try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
    cacheLoaderServer.open();

    //establish a CacheLoaderClient that a Cache can use for loading entries
    //(via the CacheLoaderServer)
    CacheLoaderClient<Integer, Integer> cacheLoader =
        new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    expiryPolicyServer.setExpiryPolicy(expiryPolicy);

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
    config.setReadThrough(true);

    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

    final Integer INITIAL_KEY = 123;
    final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;

    // set keys to read through
    Set<Integer> keys = new HashSet<>();
    for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
      keys.add(key);
    }

    // verify read-through of getValue of non-existent entries
    cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size()));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();
    closeTestCache();
  }
}