有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java什么意思Ehcache中的元素过期了

当我使用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;
}

不能告诉我是不是错了


共 (1) 个答案

  1. # 1 楼答案

    您可以使用属性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