Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Play framework中配置自定义EHCache?_Java_Ehcache_Playframework 2.4 - Fatal编程技术网

Java 如何在Play framework中配置自定义EHCache?

Java 如何在Play framework中配置自定义EHCache?,java,ehcache,playframework-2.4,Java,Ehcache,Playframework 2.4,设置: play framework 2.4.0 内置ehcache 爪哇 我已按照和的手册分离缓存,并使用我在application.conf中配置的不同配置(缓存大小、生存期等): play.cache.bindCaches = ["mycache1-cache","mycache2-cache"] 然后,为了配置它们,我创建了通常的ehcache.xml文件 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc

设置:

  • play framework 2.4.0
  • 内置ehcache
  • 爪哇
我已按照和的手册分离缓存,并使用我在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时,它可以工作,但一旦我添加自定义缓存,play就会抛出:

ProvisionException:无法设置,请参阅以下错误:1) 自定义提供程序net.sf.ehcache.ObjectExistsException中出错:缓存 mycache1缓存已存在


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

一个月前,我也遇到过同样的问题,并尝试在internet上搜索适合我的解决方案。你也可以尝试这样做

/** 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中的配置绑定命名缓存。在ehcache.xml中定义命名缓存,ehcache将在运行时自动实例化缓存。但是,Play cache插件不知道该缓存,并且无法使用{@code@NamedCache}访问该缓存。必须使用{@link CacheModule#bindCustomCache(String)}将引用绑定到命名缓存。不要将命名缓存添加到配置中的{@code play.cache.bindCaches}。以这种方式绑定的缓存由Play cache插件在运行时以编程方式添加。缓存插件将始终从默认缓存复制设置,从而限制命名缓存的用途**/
公共类CacheModule扩展了AbstractModule{
@凌驾
受保护的void configure(){
bindCustomCache(“租户”);
bindCustomCache(“用户代理”);
bindCustomCache(“地理ip”);
bindCustomCache(“完全联系”);
}
/**
*绑定在ehcache.xml中定义的命名缓存。
* 
*@param name缓存的名称。
*/
私有void bindCustomCache(字符串名称){
绑定(CacheApi.class)
.annotatedWith(新名称CacheImpl(名称))
.toProvider(新提供程序(){
@注入
CacheManager CacheManager;
@凌驾
公共缓存API get(){
Cache Cache=cacheManager.getCache(名称);
if(缓存==null){
抛出新的RuntimeException(“必须在ehcache.xml中定义名为“'+name+””的缓存”);
}
返回新的DefaultCacheApi(新的EhCacheApi(缓存));
} 
})
.asEagerSingleton();
}
}

我在ehcache.xml中创建命名缓存,只需添加一行bindCustomCache()您还需要在application.conf中设置
play.cache.createBoundCaches=false

是否完整的堆栈跟踪?
play不知道它并抛出
->play抛出什么异常?@simeon fitch no,我放弃了它,并在另一个项目中创建了自己的json文件缓存。@Navneetinggh:请同时提供导入内容。