javax.cache.integration.CacheWriter Java Examples

The following examples show how to use javax.cache.integration.CacheWriter. 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: CompositeCacheWriter.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteAll(final Collection<?> keys) throws CacheWriterException
{
    CacheWriterException e = null;
    for (final CacheWriter<K, V> writer : writers)
    {
        try
        {
            writer.deleteAll(keys);
        }
        catch (final CacheWriterException ex)
        {
            if (e == null)
            {
                e = ex;
            }
        }
    }
    if (e != null)
    {
        throw e;
    }
}
 
Example #2
Source File: CompositeCacheWriter.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(final Object key) throws CacheWriterException
{
    CacheWriterException e = null;
    for (final CacheWriter<K, V> writer : writers)
    {
        try
        {
            writer.delete(key);
        }
        catch (final CacheWriterException ex)
        {
            if (e == null)
            {
                e = ex;
            }
        }
    }
    if (e != null)
    {
        throw e;
    }
}
 
Example #3
Source File: CompositeCacheWriter.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Override
public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException
{
    CacheWriterException e = null;
    for (final CacheWriter<K, V> writer : writers)
    {
        try
        {
            writer.writeAll(entries);
        }
        catch (final CacheWriterException ex)
        {
            if (e == null)
            {
                e = ex;
            }
        }
    }
    if (e != null)
    {
        throw e;
    }
}
 
Example #4
Source File: CompositeCacheWriter.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException
{
    CacheWriterException e = null;
    for (final CacheWriter<K, V> writer : writers)
    {
        try
        {
            writer.write(entry);
        }
        catch (final CacheWriterException ex)
        {
            if (e == null)
            {
                e = ex;
            }
        }
    }
    if (e != null)
    {
        throw e;
    }
}
 
Example #5
Source File: ConfigurationMerger.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfiguration<K, V> config) {
  Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory();
  @SuppressWarnings("unchecked")
  Factory<CacheWriter<K, V>> cacheWriterFactory = (Factory<CacheWriter<K, V>>) (Object) config.getCacheWriterFactory();

  if (config.isReadThrough() && cacheLoaderFactory == null) {
    throw new IllegalArgumentException("read-through enabled without a CacheLoader factory provided");
  }
  if (config.isWriteThrough() && cacheWriterFactory == null) {
    throw new IllegalArgumentException("write-through enabled without a CacheWriter factory provided");
  }

  CacheLoader<K, V> cacheLoader = cacheLoaderFactory == null ? null : cacheLoaderFactory.create();
  CacheWriter<K, V> cacheWriter;
  try {
    cacheWriter = cacheWriterFactory == null ? null : cacheWriterFactory.create();
  } catch (Throwable t) {
    throw closeAllAfter(new CacheException(t), cacheLoader);
  }

  if (cacheLoader == null && cacheWriter == null) {
    return null;
  } else {
    return new Eh107CacheLoaderWriter<>(cacheLoader, config.isReadThrough(), cacheWriter, config.isWriteThrough());
  }
}
 
Example #6
Source File: JCacheConfigurer.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private <T> MutableConfiguration<String, T> configure(final MutableConfiguration<String, T> configuration, final OAuth2Options opts) {
    ofNullable(opts.getJcacheLoader())
            .map(n -> lookup(CacheLoader.class, n))
            .ifPresent(l -> configuration.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<CacheLoader<String, T>>(l)));
    ofNullable(opts.getJcacheWriter())
            .map(n -> lookup(CacheWriter.class, n))
            .ifPresent(w -> configuration.setCacheWriterFactory(new FactoryBuilder.SingletonFactory<CacheWriter<String, T>>(w)));
    return configuration
            .setStoreByValue(opts.isJcacheStoreValue())
            .setStatisticsEnabled(opts.isJcacheStatistics())
            .setManagementEnabled(opts.isJcacheJmx());
}
 
Example #7
Source File: CacheWriterServer.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an CacheWriterServer.
 *
 * @param port        the port on which to accept {@link CacheWriterClient} requests
 * @param cacheWriter (optional) the {@link CacheWriter} that will be used to handle
 *                    client requests
 */
public CacheWriterServer(int port, CacheWriter<K, V> cacheWriter) {
    super(port);

    // establish the client-server operation handlers
    addOperationHandler(new WriteOperationHandler());
    addOperationHandler(new WriteAllOperationHandler());
    addOperationHandler(new DeleteOperationHandler());
    addOperationHandler(new DeleteAllOperationHandler());

    this.cacheWriter = cacheWriter;
}
 
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: CaffeineConfiguration.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** @return a writer created by the configured factory or null if not set. */
public @Nullable CacheWriter<K , V> getCacheWriter() {
  if (hasCacheWriter()) {
    @SuppressWarnings("unchecked")
    CacheWriter<K , V> writer = (CacheWriter<K, V>) getCacheWriterFactory().create();
    return writer;
  }
  return null;
}
 
Example #10
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 #11
Source File: ConfigurationMergerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void jsr107WriterGetsRegistered() {
  MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>();
  CacheWriter<Object, Object> mock = mock(CacheWriter.class);
  RecordingFactory<CacheWriter<Object, Object>> factory = factoryOf(mock);
  configuration.setWriteThrough(true).setCacheWriterFactory(factory);

  merger.mergeConfigurations("cache", configuration);

  assertThat(factory.called, is(true));
  verify(cacheLoaderWriterFactory).registerJsr107Loader(eq("cache"), ArgumentMatchers.<CacheLoaderWriter<Object, Object>>isNotNull());
}
 
Example #12
Source File: Eh107CacheLoaderWriter.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
Eh107CacheLoaderWriter(CacheLoader<K, V> cacheLoader, boolean readThrough, CacheWriter<K, V> cacheWriter, boolean writeThrough) {
  this.cacheLoader = cacheLoader;
  this.readThrough = cacheLoader != null && readThrough;
  if (writeThrough) {
    this.cacheWriter = cacheWriter;
  } else {
    this.cacheWriter = null;
  }
}
 
Example #13
Source File: CacheConfiguration.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public CacheConfiguration<K, V> setCacheWriterFactory(
    Factory<? extends CacheWriter<? super K, ? super V>> factory) {
    super.setCacheWriterFactory(factory);

    return this;
}
 
Example #14
Source File: GridCacheLoaderWriterStore.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param ldr Loader.
 * @param writer Writer.
 */
GridCacheLoaderWriterStore(@Nullable CacheLoader<K, V> ldr, @Nullable CacheWriter<K, V> writer) {
    assert ldr != null || writer != null;

    this.ldr = ldr;
    this.writer = writer;
}
 
Example #15
Source File: GridCacheLoaderWriterStoreFactory.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public CacheStore<K, V> create() {
    CacheLoader<K, V> ldr = ldrFactory == null ? null : ldrFactory.create();

    CacheWriter<K, V> writer = writerFactory == null ? null : writerFactory.create();

    return new GridCacheLoaderWriterStore<>(ldr, writer);
}
 
Example #16
Source File: CacheWriterWrapper.java    From triava with Apache License 2.0 5 votes vote down vote up
public CacheWriterWrapper(CacheWriter<? super K, ? super V> cw, boolean isAsync)
	{
		@SuppressWarnings("unchecked")
		CacheWriter<K, V> cw2 = (CacheWriter<K, V>) cw;
		this.cacheWriter = cw2;
//		this.isAsync = isAsync;
	}
 
Example #17
Source File: ActionRunner.java    From triava with Apache License 2.0 5 votes vote down vote up
ActionRunner(ActionContext<K,V> actionContext)
{
	// actionContext. It is currently taken directly from the Cache
        CacheWriter<K, V> writer = actionContext.cacheWriter();
        if (writer instanceof NopCacheWriter)
        {
            this.cacheWriter = null;
        }
        else
        {               
            this.cacheWriter = actionContext.cacheWriter();
        }
	this.listeners = actionContext.listeners();
	this.stats = actionContext.statisticsCalculator();		
}
 
Example #18
Source File: GridCacheLoaderWriterStoreFactory.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param ldrFactory Loader factory.
 * @param writerFactory Writer factory.
 */
GridCacheLoaderWriterStoreFactory(@Nullable Factory<CacheLoader<K, V>> ldrFactory,
    @Nullable Factory<CacheWriter<K, V>> writerFactory) {
    this.ldrFactory = ldrFactory;
    this.writerFactory = writerFactory;

    assert ldrFactory != null || writerFactory != null;
}
 
Example #19
Source File: RepositoryConfiguration.java    From mobi with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Factory<CacheWriter<? super K, ? super V>> getCacheWriterFactory() {
    return delegate.getCacheWriterFactory();
}
 
Example #20
Source File: CacheTest.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Test
public void loader()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default", new CompleteConfiguration<Object, Object>()
    {
        /**
         * 
         */
        private static final long serialVersionUID = -4598329777808827966L;

        @Override
        public boolean isReadThrough()
        {
            return true;
        }

        @Override
        public boolean isWriteThrough()
        {
            return false;
        }

        @Override
        public boolean isStatisticsEnabled()
        {
            return false;
        }

        @Override
        public boolean isManagementEnabled()
        {
            return false;
        }

        @Override
        public Iterable<CacheEntryListenerConfiguration<Object, Object>> getCacheEntryListenerConfigurations()
        {
            return null;
        }

        @Override
        public Factory<CacheLoader<Object, Object>> getCacheLoaderFactory()
        {
            return () -> new CacheLoader<Object, Object>()
            {
                @Override
                public Object load(Object key) throws CacheLoaderException
                {
                    return "super";
                }

                @Override
                public Map<Object, Object> loadAll(Iterable<?> keys) throws CacheLoaderException
                {
                    return null;
                }
            };
        }

        @Override
        public Factory<CacheWriter<? super Object, ? super Object>> getCacheWriterFactory()
        {
            return null;
        }

        @Override
        public Factory<ExpiryPolicy> getExpiryPolicyFactory()
        {
            return null;
        }

        @Override
        public Class<Object> getKeyType()
        {
            return Object.class;
        }

        @Override
        public Class<Object> getValueType()
        {
            return Object.class;
        }

        @Override
        public boolean isStoreByValue()
        {
            return false;
        }
    });
    final Cache<String, String> cache = cacheManager.getCache("default");
    assertEquals("super", cache.get("lazilyLoaded"));
    cachingProvider.close();
}
 
Example #21
Source File: JCSConfiguration.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public Factory<CacheWriter<? super K, ? super V>> getCacheWriterFactory()
{
    return cacheWristerFactory;
}
 
Example #22
Source File: CompositeCacheWriter.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public CacheWriter<K, V> create()
{
    return this;
}
 
Example #23
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 #24
Source File: Cache.java    From triava with Apache License 2.0 4 votes vote down vote up
@Override
public CacheWriter<K, V> cacheWriter()
{
	return cacheWriter;
}
 
Example #25
Source File: CacheWriterServer.java    From cache2k with Apache License 2.0 4 votes vote down vote up
public CacheWriter<K, V> getCacheWriter() {
    return cacheWriter;
}
 
Example #26
Source File: Builder.java    From triava with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Builder<K, V> setCacheWriterFactory(Factory<? extends CacheWriter<? super K, ? super V>> factory)
{
	this.writerFactory = (Factory<CacheWriter<? super K, ? super V>>) factory;
	return this;
}
 
Example #27
Source File: CompositeCacheWriter.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
public CompositeCacheWriter(final CacheWriter<K, V>... writers)
{
    this.writers = writers;
}
 
Example #28
Source File: AsyncCacheWriter.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public CacheWriter<K, V> create()
{
    return this;
}
 
Example #29
Source File: AsyncCacheWriter.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
public AsyncCacheWriter(final CacheWriter<K, V> delegate, final int poolSize)
{
    writer = delegate;
    pool = Executors.newFixedThreadPool(
            poolSize, new DaemonThreadFactory(delegate.getClass().getName() + "-" + delegate.hashCode() + "-"));
}
 
Example #30
Source File: CopyCacheProxy.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Delegates to the wrapped cache. Wrap configuration and return true on store by value
 */
@SuppressWarnings("unchecked")
@Override
public <C extends Configuration<K, T>> C getConfiguration(Class<C> clazz) {
  final C c = cache.getConfiguration(clazz);
  if (c instanceof CompleteConfiguration) {
    final CompleteConfiguration<K, T> cc = (CompleteConfiguration<K,T>) c;
    return (C) new CompleteConfiguration<K, T>() {
      @Override
      public Iterable<CacheEntryListenerConfiguration<K, T>> getCacheEntryListenerConfigurations() {
        return cc.getCacheEntryListenerConfigurations();
      }

      @Override
      public boolean isReadThrough() {
        return cc.isReadThrough();
      }

      @Override
      public boolean isWriteThrough() {
        return cc.isWriteThrough();
      }

      @Override
      public boolean isStatisticsEnabled() {
        return cc.isStatisticsEnabled();
      }

      @Override
      public boolean isManagementEnabled() {
        return cc.isManagementEnabled();
      }

      @Override
      public Factory<CacheLoader<K, T>> getCacheLoaderFactory() {
        return cc.getCacheLoaderFactory();
      }

      @Override
      public Factory<CacheWriter<? super K, ? super T>> getCacheWriterFactory() {
        return cc.getCacheWriterFactory();
      }

      @Override
      public Factory<ExpiryPolicy> getExpiryPolicyFactory() {
        return cc.getExpiryPolicyFactory();
      }

      @Override
      public Class<K> getKeyType() {
        return cc.getKeyType();
      }

      @Override
      public Class<T> getValueType() {
        return cc.getValueType();
      }

      @Override
      public boolean isStoreByValue() {
        return true;
      }
    };
  } else if (c instanceof Configuration) {
    return (C) new Configuration<K, T>() {
      @Override
      public Class<K> getKeyType() {
        return c.getKeyType();
      }

      @Override
      public Class<T> getValueType() {
        return c.getValueType();
      }

      @Override
      public boolean isStoreByValue() {
        return true;
      }
    };
  }
  return c;
}