原始配置信息:
<cacheModel id="userCache" type="OSCACHE" readOnly="false"> <flushInterval hours="24" /> <flushOnExecute statement="User.insert" /> <flushOnExecute statement="User.update" /> <flushOnExecute statement="User.deleteByPrimaryKey" /> <flushOnExecute statement="Role.deleteRUserRoleByUserId" /> <flushOnExecute statement="Workgroup.deleteRUserWorkgroupByUserId" /> <property name="size" value="2000" /> </cacheModel> <select id="select" parameterClass="int" resultMap="result" cacheModel="userCache"> select ID, LOGIN_ID, PASSWORD, NAME, EMAIL, COMPANY_ID, STATUS, PHONE, GMT_UPDATE_PWD, CONTACT_TYPE, MOBILE, IDX_NUM from USERS <dynamic prepend="where"> <isParameterPresent> ID=#value# </isParameterPresent> </dynamic> </select>
cache使用出现问题: 在执行第一次查询的时候,cache miss,执行数据库查询,stored cache。(这是正确的执行)
在执行第2次查询时,继续提示cache miss.
设置log4j.logger.com.opensymphony.oscache=DEBUG,查看cache日志输出。结果同一条记录,stored cache与 miss cache使用的key居然不一致。跟踪底层的代码,发现在生成cache key的时候,使用了对象的hashcode,故做如下的修正:
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof User)) {
return false;
}
User u = (User) obj;
if (u.getId() != null && getId() != null)
return u.getId().equals(getId());
return super.equals(obj);
}
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
问题依然没有得到解决,郁闷 ~.~!
纠结了好久,终于找到问题的原因,在保证如上重写 hashcode的基础上,修改配置文件
<cacheModel id="userCache" type="OSCACHE" readOnly="true">
cache终于能正常使用。
本文探讨了在使用OSCACHE缓存过程中遇到的问题,包括缓存未能正常工作、缓存key不一致等,并给出了通过调整缓存配置和重写hashCode方法来解决问题的方法。
1338

被折叠的 条评论
为什么被折叠?



