EhCache在java中默认缓存

我有ehCache的配置:

   

如何访问EhCache的默认缓存?

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

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

CacheManager.getCache只会在已经创建的情况下返回缓存实例,因此您需要使用addCacheIfAbsent()addCacheIfAbsent()告诉它创建一个新缓存实例。 名称无关紧要,它将使用默认缓存设置按需创建。

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

使用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; } 

它不是threadSafe(使用TestNg并发测试进行测试)。 最终实现如下:

 private final static Map 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()); } 

请参考ehcache的api

addCache

public void addCache(String cacheName)

  throws IllegalStateException, ObjectExistsException, CacheException 

基于具有给定名称的defaultCache添加Ehcache。

来自: http : //ehcache.org/apidocs/2.7.6/

希望能帮到你!

但是如果我们添加一个名为’default’的区域,ehcache将抛出exception:

net.sf.ehcache.ObjectExistsException:已配置默认缓存