2010-12-15 46 views
1

当我使用Ehcache时,有些东西我无法理解,意味着元素已过期,如果元素过期,这意味着缓存将清除元素,并且它将不再存在?或者它也存在,但你无法得到它。这里是我写的代码,我从XML获取数据并且判断像这样什么意思是Ehcache中过期的元素

public Object get(Class classObj, String nodeName, String fileName) { 
    Object obj = null; 

    if (!ehcacheVindicator.getCache().isDisabled()&&ehcacheVindicator.getCache().isKeyInCache(nodeName)) { 
     Element element = ehcacheVindicator.getCache().get(nodeName); 
     if (ehcacheVindicator.getCache().isExpired(element)){ 
      obj = readObject(classObj, fileName, nodeName);// read object from xml file 
      updateObject(nodeName,obj); 
     } 
     else 
      obj = getObject(nodeName); // get object from cache 
    } else { 
     obj = readObject(classObj, fileName, nodeName); // read object from 
     // xml file 
     addObject(nodeName, obj); // add object to cache 
    } 
    return obj; 
} 

不能告诉我它是否错误?

回答

1

你可以设置你的Ehcache缓存对象的时间与属性timeToLiveSeconds(XML配置文件中)在有限时间内

timeToLiveSeconds: 设置消亡前的生活为元素的时间。 即创建时间与元素 到期之间的最长时间。 仅当元素不是永恒时才使用。 可选属性。值为0意味着元素可以活到 无限。 默认值为0。

更多信息here