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

The following examples show how to use javax.cache.configuration.MutableConfiguration#setCacheWriterFactory() . 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: CacheWriterTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Configure write-through before each test.
 */
@Before
public void onBeforeEachTest() throws IOException {
  // establish and open a CacheWriterServer to handle cache
  // cache loading requests from a CacheWriterClient
  cacheWriter = new RecordingCacheWriter<>();
  cacheWriterServer = new CacheWriterServer<>(10000, cacheWriter);
  cacheWriterServer.open();

  // establish the CacheManager for the tests
  cacheManager = Caching.getCachingProvider().getCacheManager();

  // establish a CacheWriterClient that a Cache can use for writing/deleting entries
  // (via the CacheWriterServer)
  CacheWriterClient<Integer, String> theCacheWriter = new CacheWriterClient<>(cacheWriterServer.getInetAddress(),
      cacheWriterServer.getPort());

  MutableConfiguration<Integer, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Integer.class, String.class);
  configuration.setCacheWriterFactory(FactoryBuilder.factoryOf(theCacheWriter));
  configuration.setWriteThrough(true);

  getCacheManager().createCache("cache-writer-test", configuration);
  cache = getCacheManager().getCache("cache-writer-test", Integer.class, String.class);
}
 
Example 2
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 3
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 4
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 5
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);
    }
}