Ehcache怎样在Element级上控制过期时间
CacheManager ehCacheManager = new CacheManager();
ehCacheManager.addCache("default");
Ehcache cache = ehCacheManager.getCache("default");
Element e = new Element("aa", "aa", false, 1, 1);
cache.put(e);
System.out.println(cache.get("aa"));
Thread.sleep(1050);
System.out.println(cache.get("aa"));//如果这个时候,期待cache是否过期。但是实际的情况是。ehcache依然能获取到相关数据.
?
当你去调用ehcache.put动作时,会调用applyDefaultsToElementWithoutLifespanSet(element);
方法内容:
?if (!element.isLifespanSet()) {
??????????? //Setting with Cache defaults
??????????? element.setTimeToLive((int) configuration.getTimeToLiveSeconds());
??????????? element.setTimeToIdle((int) configuration.getTimeToIdleSeconds());
??????????? element.setEternal(configuration.isEternal());
??????? }
?
?
Element里面有一个isLifespan的参数,默认是为false的。。
false的时候.Ehcache会element的过期时间设置为默认配置的
当你通过new? Element(Object key, Object value,
?????????????????? boolean eternal, int timeToIdleSeconds, int timeToLiveSeconds)
去实例化的时候。。根本不会去设置isLifespan这个参数,而是采用默认的过期策略的。。
但是去调用element 的。setTimeToLive,setTimeToIdle,setEternal方法时,
确会去设置这个参数。。
Element.setEternal(boolean eternal) {
??????? this.eternal = eternal;
??????? lifespanSet = true;
??? }
这样的话。。就会去单独去设置Element控制过期时间,而不会用默认的配置去覆盖设置.
?
?
个人觉得这个ehcache存在的一个BUG。。
既然在调用element 的。setTimeToLive,setTimeToIdle,setEternal方法时,会去设置这个参数,那么如果在构造的时候也应该调用这些方法。
但是有可能ehcache希望用户能根据不同cache config去配置应用。而不应该应用到element级别上
?
?
1 楼 ZetaGundam 2011-03-29 请教一下,LZ的这个测试是在ehcache的什么版本上进行的?