java中的EhCache默认缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2961070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EhCache default cache in java
提问by user349302
I have this configuration for ehCache:
我有 ehCache 的这个配置:
<ehcache>
<defaultCache
name="defaut"
maxElementsInMemory="5"
eternal="false"
timeToIdleSeconds="20"
timeToLiveSeconds="20"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
How can I get access to default cache of EhCache?
如何访问 EhCache 的默认缓存?
CacheManager.getInstance().getCache("default"); // returns null
采纳答案by skaffman
My understanding is that the "default cache" is actually a template for new caches that get created, rather than being a specific named cache.
我的理解是“默认缓存”实际上是创建的新缓存的模板,而不是特定的命名缓存。
CacheManager.getCachewill only return a cache instance if it has already been created, so you'll need to tell it to create a new one, using something like addCacheIfAbsent(). The name doesn't matter, it will be created on demand using the default cache settings.
CacheManager.getCache如果它已经被创建,只会返回一个缓存实例,所以你需要告诉它创建一个新的,使用类似addCacheIfAbsent(). 名称无关紧要,它将使用默认缓存设置按需创建。
回答by NS du Toit
I came across the same problem trying to create a new cache.
我在尝试创建新缓存时遇到了同样的问题。
Using EhCache 2.7.3 I couldn't use the addCacheIfAbsent(..)method since it returns the deprecated EhCache class, and not the Cache class.
使用 EhCache 2.7.3 我无法使用该addCacheIfAbsent(..)方法,因为它返回已弃用的 EhCache 类,而不是 Cache 类。
My initial attempt was as follows:
我最初的尝试如下:
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;
}
But using CacheManager.getCache("default")returns null- so yeah, it doesn't seem like one can get a reference to the default (template) cache.
但是使用CacheManager.getCache("default")返回null- 所以是的,似乎无法获得对默认(模板)缓存的引用。
I got the code working as follows:
我让代码工作如下:
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;
}
It wasn't thread safe (tested using TestNg concurrent tests). The final implementation looks as follows:
它不是线程安全的(使用 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());
}
回答by u4935936
please refer to the api of ehcache
请参考ehcache的api
addCache
public void addCache(String cacheName)
throws IllegalStateException, ObjectExistsException, CacheExceptionAdds a Ehcache based on the defaultCache with the given name.
添加缓存
public void addCache(String cacheName)
throws IllegalStateException, ObjectExistsException, CacheException添加基于具有给定名称的 defaultCache 的 Ehcache。
from: http://ehcache.org/apidocs/2.7.6/
来自:http: //ehcache.org/apidocs/2.7.6/
hope to help you!
希望能帮到你!

