Java 中的 EhCache 默认缓存

2022-09-03 05:52:05

我有这个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

答案 1

我的理解是,“默认缓存”实际上是创建的新缓存的模板,而不是特定的命名缓存。

CacheManager.getCache仅当缓存实例已创建时,才会返回缓存实例,因此您需要告诉它使用类似 .名称无关紧要,它将使用默认缓存设置按需创建。addCacheIfAbsent()


答案 2

我在尝试创建新缓存时遇到了同样的问题。

使用EhCache 2.7.3,我无法使用该方法,因为它返回已弃用的EhCache类,而不是Cache类。addCacheIfAbsent(..)

我最初的尝试如下:

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://stackoverflow.com/questions/27753184/java-hashmap-add-new-entry-while-iterating)
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());
}

推荐