ibatis oscache 运用中miss cache
ibatis oscache 使用中miss cache
原始配置信息:
<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终于能正常使用。
1 楼
guanhw
2011-01-13
说说看 你的jdk 是什么版本, oscache,ibaits 是什么版本???
2 楼
cn-done
2011-01-19
JDK 1.5.0_14
ibatis-2.3.4.726.jar
oscache-2.4.1.jar
ibatis-2.3.4.726.jar
oscache-2.4.1.jar