如何在Play框架中配置自定义ehcaches?

时间:2015-08-06 13:37:22

标签: java ehcache playframework-2.4

设定:

  • play framework 2.4.0
  • 内置ehcache
  • 的java

我已按照https://www.playframework.com/documentation/2.4.0/JavaCache上的手册进行操作,并分离缓存并使用我在application.conf中配置的不同配置(缓存大小,生命周期等):

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

然后,为了配置它们,我创建了通常的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>

当我只保留defaultCache时它会起作用,但是一旦我添加了自定义缓存,就会抛出以下内容:

  

ProvisionException:无法配置,请参阅以下错误:1)   自定义提供程序中的错误net.sf.ehcache.ObjectExistsException:Cache   mycache1-cache已经存在

但是,如果我只在ehcache.xml中定义缓存而不是在application.conf中定义缓存,则play不知道它并抛出。

2 个答案:

答案 0 :(得分:0)

我一个月前也遇到过同样的问题,并尝试通过互联网搜索以及适合我的解决方案。你也可以尝试做同样的事情。

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

我在ehcache.xml中创建了我的命名缓存,只需要在configure方法中添加一行bindCustomCache()。

答案 1 :(得分:0)

您还需要在application.conf中设置play.cache.createBoundCaches = false