当我使用Ehcache时,有一点我无法理解,元素过期是什么意思,如果元素过期就意味着缓存将清除该元素并且不再存在?或者它也存在但你无法得到它。这是我写的代码,我从xml获取数据并这样判断
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;
}
无法告诉我这是否是错误的?
最佳答案
您可以使用属性timeToLiveSeconds
(在xml配置文件中)将ehcache设置为在有限的时间内缓存对象
timeToLiveSeconds: Sets the time to live for an element before it expires. i.e. The maximum time between creation time and when an element expires. Is only used if the element is not eternal. Optional attribute. A value of 0 means that and Element can live for infinity. The default value is 0.
更多信息here
关于java - Ehcache 中的元素过期是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4447733/