2015-08-06 15 views
9

Setup:Come configurare ehcaches personalizzati in Play framework?

  • quadro gioco 2.4.0
  • built-in EHCache
  • java

ho seguito il manuale a https://www.playframework.com/documentation/2.4.0/JavaCache e di separare le cache e utilizzare diverse configurazioni (dimensioni della cache , ecc.) Configura in application.conf:

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

Poi, configurarli, ho creato il solito file di ehcache.xml

<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> 

Funziona quando Ho solo mantenere il defaultCache, ma non appena aggiungo la cache personalizzato, il gioco getta con:

ProvisionException: impossibile disposizione, vedere i seguenti errori: 1) errore nel provider personalizzato, net.sf.ehcache.ObjectExistsException: cache-cache mycache1 esiste già

Tuttavia, se definisco solo la cache in ehcache.xml ma non in application.conf, play non ne sa e getta.

+0

È lo stacktrace completo? – dan

+0

'play non lo sa e getta' -> quale eccezione viene lanciata dal gioco? –

+0

Hai fortuna con questo? – metasim

risposta

0

Ho anche affrontato lo stesso problema un mese fa e ho provato a cercare su Internet e sotto la soluzione che funziona per me. Puoi anche provare a fare lo stesso.

/** 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(); 
    } 
} 

Creo il mio cache denominati in ehcache.xml e solo di aggiungere una sola riga bindCustomCache() per il metodo di configurazione.