How to configure custom ehcaches in Play framework?
Asked Answered
G

2

11

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.

Glochidium answered 6/8, 2015 at 13:37 Comment(3)
Is that the full stacktrace?Ruyle
play does not know about it and throws -> what exception get thrown by play?Pampero
@simeon-fitch no, I dropped it and made my own json file cache in a separate project.Glochidium
S
1

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.

Screeching answered 22/7, 2016 at 9:34 Comment(1)
@navneetsingh : Pls give the imports also.Ringer
T
0

You will also need to set play.cache.createBoundCaches = false in application.conf.

Taligrade answered 24/9, 2018 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.