【问题标题】:EhCache default cache in javajava中的EhCache默认缓存
【发布时间】:2010-06-02 19:45:31
【问题描述】:

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") 抛出“默认缓存已配置”。

标签: java ehcache


【解决方案1】:

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

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://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());
    }
    

    【讨论】:

      【解决方案3】:

      请参考ehcache的api

      添加缓存

      public void addCache(String cacheName)

                throws IllegalStateException,
                       ObjectExistsException,
                       CacheException  
      

      基于给定名称的 defaultCache 添加一个 Ehcache。

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

      希望能帮到你!

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多