Do caches in ehcache.xml inherit from defaultCache?

喜夏-厌秋 提交于 2020-01-01 08:03:28

问题


If I have the following configuration:

<defaultCache timeToIdleSeconds="120"
        timeToLiveSeconds="120" />
<cache name="test"
        timeToLiveSeconds="300" />

What will be the value of timeToIdleSeconds for the cache test? Will it be inherited from the default cache, and thus be equal to 120, or will it take the default value as given in the manual, which is 0 (infinity)?


回答1:


The timeToIdleSeconds will be default value and not inherit from "defaultCache". The "defaultCache" is a bit misnomer/misleading, in the sense that it does not provide "defaults" for every cache, but its just a way of specifying config for caches that can/are added dynamically - using cacheManager.addCache(String cacheName).

From http://www.ehcache.org/ehcache.xml, documentation for that tag reads

Default Cache configuration. 
These settings will be applied to caches created programmatically using
 CacheManager.add(String cacheName). This element is optional, and using
 CacheManager.add(String cacheName) when its not present will throw CacheException
 The defaultCache has an implicit name "default" which is a reserved cache name.



回答2:


private Ehcache cloneDefaultCache(final String cacheName) {
        if (defaultCache == null) {
            return null;
        }
        Ehcache cache;
        try {
            cache = (Ehcache) defaultCache.clone();
        } catch (CloneNotSupportedException e) {
            throw new CacheException("Failure cloning default cache. Initial cause was " + e.getMessage(), e);
        }
        if (cache != null) {
            cache.setName(cacheName);
        }
        return cache;
    }
Method
    cloneDefaultCache(String)
Found usages  (2 usages found)
    Library  (2 usages found)
        Unclassified usage  (2 usages found)
            Maven: net.sf.ehcache:ehcache-core:2.6.11  (2 usages found)
                net.sf.ehcache  (2 usages found)
                    CacheManager  (2 usages found)
                        addCache(String)  (1 usage found)
                            1173 Ehcache clonedDefaultCache = cloneDefaultCache(cacheName);
                        addCacheIfAbsent(String)  (1 usage found)
                            1857 Ehcache clonedDefaultCache = cloneDefaultCache(cacheName);


来源:https://stackoverflow.com/questions/10931942/do-caches-in-ehcache-xml-inherit-from-defaultcache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!