我在应用程序开始时对对象进行缓存,此对象是解析xml文件并将其内容存储在ArrayList
中的结果。
现在我更新了xml文件中的数据,但是缓存中的对象仍然具有旧值,这里是我称之为缓存对象的代码:
ObjectsCaching objCache = ObjectsCaching.getObjectsCaching();
Cache cache = objCache.getMemoryCache();
List<FormPorperties> formList = null; //to obtain form properties from xml file whether form cashed object or through parsing xml file
if (cache.get("formValidators") == null) {//if object is not cashed in memory
try {
InputStream fis = this.getClass().getResourceAsStream("FormFieldsNames.xml");
// list of FormPropereties, each formProperties object contain form name
// and HashMap of field with its validation
formList = XMLFormParsing.getFormsProperties(fis); //singelton class
cache.put("formProperties", formList); //cash object
} catch (IOException ex) {
Logger.getLogger(ContextListener.class.getName()).log(Level.SEVERE, "Error in parsing FormFieldsNames file ", ex);
} catch (XMLStreamException ex) {
Logger.getLogger(ContextListener.class.getName()).log(Level.SEVERE, "Error in parsing FormFieldsNames file ", ex);
}
} else {
formList = (List<FormPorperties>) cache.get("formProperties");
}
我尝试删除支票代码
if (cache.get("formValidators") == null)
强制应用程序解析文件,但它仍然读取旧内容
xml文件的内容是表单名称和每个表单字段,我用这种方式对服务器端的表单字段进行验证。
这是conetn
<form name="groupAdditoinEditionForm">
<fieldName > groupEname </fieldName>
<fieldName > groupAname </fieldName>
</form>
<form name="userAdditionFrom">
<fieldName > userName </fieldName>
<fieldName > country </fieldName>
<fieldName > faxNo </fieldName>
<fieldName > email </fieldName>
<fieldName > confirmEmail </fieldName>
<fieldName > accessControl </fieldName>
<fieldName > lang </fieldName>
<fieldName > group </fieldName>
</form>
我将第一个标记添加到xml文件中,但解析器仍然读取旧值!!
答案 0 :(得分:0)
这可能是问题的一部分:
// Uses key "formValidators"
if (cache.get("formValidators") == null))
...
// Uses key "formProperties"
formList = (List<FormPorperties>) cache.get("formProperties");
肯定你两次都应该使用相同的密钥?或者(理想情况下)只需抓取一次,然后只有formList
仍然为空时才会通过重新填充部分......就像这样:
// Adjust "key" to whatever cache key you *really* want to use
List<FormPorperties> formList = (List<FormPorperties>) cache.get("key");
if (formList == null) {
// Populate and cache
}
现在为什么值没有被更新...你是否正在做任何使缓存无效的事情?如果没有,你期望它会更新值?系统的任何部分是否都在监视XML文件,以便从缓存中删除过时的条目?