Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure custom ehcaches in Play framework?

Setup:

  • play framework 2.4.0
  • built-in ehcache
  • java

I have followed the manual at https://www.playframework.com/documentation/2.4.0/JavaCache and to separate caches and use different configs (cache sizes, lifetimes etc) I configure in application.conf:

play.cache.bindCaches = ["mycache1-cache","mycache2-cache"]

Then, to configure them, I created the usual ehcache.xml file

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">

    <defaultCache
        maxBytesLocalHeap="256000000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
        maxElementsOnDisk="10000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        />

    <cache name="mycache1-cache"
            maxBytesLocalHeap="256000000"
            eternal="false"
            timeToIdleSeconds="86400"
            timeToLiveSeconds="86400"
            overflowToDisk="true"
            maxElementsOnDisk="1000000"
            diskPersistent="true"
            diskExpiryThreadIntervalSeconds="1200"
            memoryStoreEvictionPolicy="LRU"
            />

</ehcache>

It works when I only keep the defaultCache, but as soon as I add the custom cache, play throws with:

ProvisionException: Unable to provision, see the following errors: 1) Error in custom provider, net.sf.ehcache.ObjectExistsException: Cache mycache1-cache already exists

However, if I only define the cache in ehcache.xml but not in application.conf, play does not know about it and throws.

like image 604
Adrian Pop Avatar asked Aug 06 '15 13:08

Adrian Pop


People also ask

How does caffeine cache work?

Caffeine provides flexible construction to create a cache with a combination of the following optional features: automatic loading of entries into the cache, optionally asynchronously. size-based eviction when a maximum is exceeded based on frequency and recency.


1 Answers

I have also faced the same issue a month ago and tried to search over internet and below solution that works for me. You can also try to do same.

/** Bind named caches using configuration in ehcache.xml. Define a named cache in ehcache.xml and Ehcache will automatically instantiate the cache at runtime. However,this cache is unknown to the Play cache plugin and is not accessible using {@code @NamedCache}. You must bind a reference to the named cache using {@link CacheModule#bindCustomCache(String)}. Do not add the named cache to {@code play.cache.bindCaches} in configuration. Caches bound in this manner are programmatically added by the Play cache plugin at runtime. The cache plugin will always copy settings from the default cache, limiting the usefulness of named caches. **/

public class CacheModule extends AbstractModule {

@Override
protected void configure() {
    bindCustomCache("tenants");
    bindCustomCache("user-agent");
    bindCustomCache("geo-ip");
    bindCustomCache("full-contact");
}

/**
 * Bind a named cache that is defined in ehcache.xml.
 * 
 * @param name The name of the cache.
 */
private void bindCustomCache(String name) {
    bind(CacheApi.class)
            .annotatedWith(new NamedCacheImpl(name))
            .toProvider(new Provider<CacheApi>() {

                @Inject
                CacheManager cacheManager;

                @Override
                public CacheApi get() {
                    Cache cache = cacheManager.getCache(name);
                    if (cache == null) {
                        throw new RuntimeException("Cache named '" + name + "' must be defined in ehcache.xml");
                    }
                    return new DefaultCacheApi(new EhCacheApi(cache));
                } 

            })
            .asEagerSingleton();
  }
}

I create my named caches in ehcache.xml and just have to add a one line bindCustomCache() to the configure method.

like image 130
Naveen Singh Avatar answered Oct 24 '22 15:10

Naveen Singh