Java中的EhCache默认缓存

16

我有这个 ehCache 的配置:

<ehcache>
    <defaultCache
            name="defaut"
            maxElementsInMemory="5"
            eternal="false"
            timeToIdleSeconds="20"
            timeToLiveSeconds="20"
            overflowToDisk="false"
            diskPersistent="false"
            memoryStoreEvictionPolicy="LRU"
            />           
</ehcache>

我该如何访问EhCache的默认缓存?

CacheManager.getInstance().getCache("default"); // returns null

这些设置将应用于通过编程方式创建的缓存 - _ehcache-failsafe.xml_。
defaultCache 元素没有 name 属性 - _ehcache.xsd_。
顺便说一下,CacheManager.getInstance().addCache("default") 抛出 "The Default Cache has already been configured"。
- cubanacan
3个回答

23

我理解"默认缓存"实际上是创建新缓存的模板,而不是具体命名的缓存。

CacheManager.getCache 只有在已经创建了缓存实例时才会返回该缓存实例,因此您需要告诉它创建一个新的缓存实例,例如使用 addCacheIfAbsent()。名称并不重要,它将根据默认缓存设置按需创建。


2
我遇到了同样的问题,尝试创建一个新的缓存。
使用EhCache 2.7.3时,我无法使用addCacheIfAbsent(..)方法,因为它返回已弃用的EhCache类,而不是Cache类。
我的初始尝试如下:
private Cache addCache(Class<?> cacheClazz) {
    CacheConfiguration cacheConfiguration = null;
    {
        Cache defaultCache = getCacheManager().getCache("default");
        cacheConfiguration = defaultCache.getCacheConfiguration();
    }
    Cache newCache = new Cache(cacheConfiguration);
    getCacheManager().addCache(newCache);
    return newCache;
}

但是使用 CacheManager.getCache("default") 返回 null - 所以,似乎无法获取默认(模板)缓存的引用。

我将代码修改如下,现在可以正常工作:

private Cache addCache(Class<?> cacheClazz) {
    // get the default (template) cache configuration
    CacheConfiguration cacheConfiguration = getCacheManager().getConfiguration().getDefaultCacheConfiguration();
    // give it a unique name or the process will fail
    cacheConfiguration.setName(cacheClazz.getName());
    Cache newCache = new Cache(cacheConfiguration);
    getCacheManager().addCache(newCache);
    return newCache;
}

在使用TestNg并发测试时,它不是线程安全的。最终的实现如下:

private final static Map<String, String> firstRunMap = new HashMap<>(); // Not using ConcurrentHashMap so be careful if you wanted to iterate over the Map (https://dev59.com/ml4c5IYBdhLWcg3wl7Mg)
private Cache addCache(Class<?> cacheClazz) {
    final String mapKey = getMapKey(cacheClazz);

    if (firstRunMap.get(mapKey) == null) {
        synchronized(mapKey) {
            if (firstRunMap.get(mapKey) == null) {

                // -----------------------------------------------------
                // First run for this cache!!!
                // -----------------------------------------------------

                // get the default (template) cache configuration
                CacheConfiguration cacheConfiguration = getCacheManager().getConfiguration().getDefaultCacheConfiguration();
                // give it a unique name or the process will fail
                cacheConfiguration.setName(cacheClazz.getName());
                Cache newCache = new Cache(cacheConfiguration);
                getCacheManager().addCache(newCache);

                // -----------------------------------------------------
                // First run complete!!!
                // -----------------------------------------------------

                firstRunMap.put(mapKey, "");

                return newCache;
            }
        }
    }

    // Not the first thread
    return getCache(cacheClazz);
}

    // This class is AbstractEhCache - change it to your class
private String getMapKey(Class<?> cacheClazz) {
    String mapKey = AbstractEhCache.class.getName() // to differentiate from similar keys in other classes
            + "-" + cacheClazz.getName();
    // Using intern() on the key as I want to synchronize on it.
    // (Strings with different hashCodes represent different locks)
    return mapKey.intern();
}

private Cache getCache(Class<?> cacheClazz) {
    return getCacheManager().getCache(cacheClazz.getName());
}

0
请参考Ehcache的API。

addCache

public void addCache(String cacheName)

          throws IllegalStateException,
                 ObjectExistsException,
                 CacheException  

Adds a Ehcache based on the defaultCache with the given name.

from: http://ehcache.org/apidocs/2.7.6/

希望能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接