首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Ehcache怎样在Element级下控制过期时间

2012-11-15 
Ehcache怎样在Element级上控制过期时间CacheManager ehCacheManager new CacheManager()ehCacheManager

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的什么版本上进行的?
我这边正好遇到一个类似的问题。
对new element()的时候把TTI和TTL都给初始化成0了,eternal初始化为false,并放入缓存中
cache本身的初始化为eCM.createCache("xxxxx", 100000, false, false, 100, 100,re);
然后我自己完成了listener捕获element过期,但是从来没有打出过我期待看到的element过期信息,但是其他的element过期信息我能够正常接收,请问LZ测试的这个BUG是随机发生还是必发生?

热点排行