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

标签 java ehcache playframework-2.4

设置:

  • 玩框架2.4.0
  • 内置缓存
  • 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 时它可以工作,但是一旦我添加了自定义缓存,就可以玩 throws 了:

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

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

最佳答案

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

/** 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 中创建我的命名缓存,只需在配置方法中添加一行 bindCustomCache()。

关于java - 如何在 Play 框架中配置自定义 ehcaches?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31857301/

相关文章:

java - 应用程序版本更新片段

java - 在ejabberd中处理异步消息

java - EHCache 3.5 获取所有缓存键/条目

hibernate - 何时/如何以编程方式设置 Hibernate 使用的 Ehcache 大小

scala - 如何在 Play2.4/Scala 中自动订阅 actor 到 akka 事件总线

java - Vaadin 6 上传组件 : how to terminate file upload proccess without saving wrong file on a server

java - 指向桌面 (Java) 和发布程序的定向路径

java - 防止 ehcache 在启动时尝试访问互联网

Scala Play Framework - Controller 作为类或单例

Java Play 2.4 和 IDEA - View 无法解析