cachemodel是ibatis里面自带的缓存机制,正确的应用能很好提升我们系统的性能。
使用方法:在sqlmap的配置文件中加入
<cacheModel type="LRU" id="vclass_cache" readOnly="true" serialize="false">
<flushInterval hours="24"/>
<flushOnExecute statement="insertVclass"/>
<flushOnExecute statement="batchInsertVclass"/>
<flushOnExecute statement="removeVclass"/>
<property name="cache_size" value="50"/>
</cacheModel>
在查询语句中使用缓存
<select id="getVclassList" parameterClass="com.wizincloud.vclass.model.Vclass" resultClass="com.wizincloud.vclass.model.Vclass" cacheModel="vclass_cache">
SELECT * FROM vclass
<include refid="getVclassList_body" />
order by created_at desc
<dynamic prepend="">
<isNotNull property="rowNumStart">
<isNotNull property="pageSize">
LIMIT #rowNumStart#,#pageSize#
</isNotNull>
</isNotNull>
</dynamic>
</select>
cacheModel 用在count语句中没有作用。
先对cachemodel中一些用法进行解释一下:
type:是缓存的类型,ibatis中有4种方式,分别为MEMORY、LRU、FIFO、OSCACHE
MEMORY是内存缓存,和Java内存管理机制类似分为[SOFT、WEAK、STRONG]
LRU是使用最近最少使用策略
FIFO是使用先进先出策略
OSCACHE是通过第三方的缓存插件实现
id:是cachemodel的一个标识
readOnly:指缓存的数据对象是只读还是可读写,默认只读,这里的只读并不是意味着数据对象一旦放入缓存中就无法再对数据进行修改。而是当数据对象发生变化的时候,如数据对象的某个属性发生了变化,则此数据对象就将被从缓存中废除,下次需要重新从数据库读取数据,构造新的数据对象。而readOnly=”false”则意味着缓存中的数据对象可更新.
serialize:只有在readOnly为false的情况下才生效,因为在readOnly为true时,所有数据对象相同,只有可读写时,才会出现不同的session拥有不同的数据对象副本。
flushInterval:指定缓存自动刷新的时间,可以为hours,minutes,seconds,milliseconds.
flushOnExecute:指定在发生哪些操作时,更新缓存。
property:不同type的cachemodel有不同的属性。
(1)当readOnly=true ,serialize=false,只指定insert更新缓存时,我在update一个article的属性后,发现之前的相同的查询条件查询结果是缓存的内容,没取到更新的结果;当我改变查询条件时,出现了更新的结果。由此可见当readonly=true,查询条件不变时,即使发生更新,缓存仍然有效;而查询条件变更时,缓存失效,用新的sql去数据库查询。当发生insert操作时,不管查询条件是否变化,都重新去数据库查询,缓存被更新。
(2)当readOnly=false,serialize=false,只指定insert更新缓存时,update一个article属性后,缓存失效,重新去数据库查询
综上来看,对更新比较频繁的数据,使不使用感觉效率差不多,对更新不是很频繁的数据,使用缓存效率还是能提升的!
实验结果:
第一次执行查询:
2015-08-18 10:10:05,259 DEBUG [java.sql.Connection,27] - {conn-100020} Connection
2015-08-18 10:10:05,262 DEBUG [com.ibatis.sqlmap.engine.cache.CacheModel,27] - Cache ‘Vclass.vclass_cache‘: cache miss
2015-08-18 10:10:05,263 DEBUG [java.sql.Connection,27] - {conn-100020} Preparing Statement: SELECT * FROM vclass WHERE wxAccountId=? order by created_at desc
2015-08-18 10:10:05,265 DEBUG [java.sql.PreparedStatement,27] - {pstm-100021} Executing Statement: SELECT * FROM vclass WHERE wxAccountId=? order by created_at desc
2015-08-18 10:10:05,265 DEBUG [java.sql.PreparedStatement,27] - {pstm-100021} Parameters: [2]
2015-08-18 10:10:05,266 DEBUG [java.sql.PreparedStatement,27] - {pstm-100021} Types: [java.lang.Integer]
2015-08-18 10:10:05,275 DEBUG [java.sql.ResultSet,27] - {rset-100022} ResultSet
2015-08-18 10:10:05,278 DEBUG [java.sql.ResultSet,27] - {rset-100022} Header: [id, classname, type, offtime, stu_num, description, status, created_at, updated_at, wxAccountId, userId]
2015-08-18 10:10:05,279 DEBUG [java.sql.ResultSet,27] - {rset-100022} Result: [10, 123123, 0, 2015-08-26 00:00:00.0, 123, <p>123123123</p>, 0, 2015-08-18 00:00:00.0, null, 2, 2]
2015-08-18 10:10:05,280 DEBUG [java.sql.ResultSet,27] - {rset-100022} Result: [9, 招生啦, 0, 2015-08-12 00:00:00.0, 2, <p>阿迪萨斯</p>, 2, 2015-08-12 00:00:00.0, null, 2, 2]
2015-08-18 10:10:05,280 DEBUG [com.ibatis.sqlmap.engine.cache.CacheModel,27] - Cache ‘Vclass.vclass_cache‘: stored object ‘[[email protected], [email protected]]‘
2015-08-18 10:10:05,316 DEBUG [java.sql.Connection,27] - {conn-100023} Connection
2015-08-18 10:10:05,317 DEBUG [java.sql.Connection,27] - {conn-100023} Preparing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:05,318 DEBUG [java.sql.PreparedStatement,27] - {pstm-100024} Executing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:05,319 DEBUG [java.sql.PreparedStatement,27] - {pstm-100024} Parameters: [10]
2015-08-18 10:10:05,320 DEBUG [java.sql.PreparedStatement,27] - {pstm-100024} Types: [java.lang.Integer]
2015-08-18 10:10:05,330 DEBUG [java.sql.ResultSet,27] - {rset-100025} ResultSet
2015-08-18 10:10:05,331 DEBUG [java.sql.ResultSet,27] - {rset-100025} Header: [COUNT(*)]
2015-08-18 10:10:05,332 DEBUG [java.sql.ResultSet,27] - {rset-100025} Result: [0]
2015-08-18 10:10:05,355 DEBUG [java.sql.Connection,27] - {conn-100026} Connection
2015-08-18 10:10:05,356 DEBUG [java.sql.Connection,27] - {conn-100026} Preparing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:05,356 DEBUG [java.sql.PreparedStatement,27] - {pstm-100027} Executing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:05,357 DEBUG [java.sql.PreparedStatement,27] - {pstm-100027} Parameters: [9]
2015-08-18 10:10:05,357 DEBUG [java.sql.PreparedStatement,27] - {pstm-100027} Types: [java.lang.Integer]
2015-08-18 10:10:05,365 DEBUG [java.sql.ResultSet,27] - {rset-100028} ResultSet
2015-08-18 10:10:05,366 DEBUG [java.sql.ResultSet,27] - {rset-100028} Header: [COUNT(*)]
2015-08-18 10:10:05,366 DEBUG [java.sql.ResultSet,27] - {rset-100028} Result: [0]
2015-08-18 10:10:05,393 DEBUG [java.sql.Connection,27] - {conn-100029} Connection
2015-08-18 10:10:05,394 DEBUG [com.ibatis.sqlmap.engine.cache.CacheModel,27] - Cache ‘Vclass.vclass_cache‘: cache miss
2015-08-18 10:10:05,394 DEBUG [java.sql.Connection,27] - {conn-100029} Preparing Statement: SELECT COUNT(*) FROM vclass WHERE wxAccountId=?
2015-08-18 10:10:05,394 DEBUG [java.sql.PreparedStatement,27] - {pstm-100030} Executing Statement: SELECT COUNT(*) FROM vclass WHERE wxAccountId=?
2015-08-18 10:10:05,395 DEBUG [java.sql.PreparedStatement,27] - {pstm-100030} Parameters: [2]
2015-08-18 10:10:05,395 DEBUG [java.sql.PreparedStatement,27] - {pstm-100030} Types: [java.lang.Integer]
2015-08-18 10:10:05,404 DEBUG [java.sql.ResultSet,27] - {rset-100031} ResultSet
2015-08-18 10:10:05,405 DEBUG [java.sql.ResultSet,27] - {rset-100031} Header: [COUNT(*)]
2015-08-18 10:10:05,405 DEBUG [java.sql.ResultSet,27] - {rset-100031} Result: [2]
2015-08-18 10:10:05,405 DEBUG [com.ibatis.sqlmap.engine.cache.CacheModel,27] - Cache ‘Vclass.vclass_cache‘: stored object ‘2‘
第二次执行查询:
2015-08-18 10:10:20,985 DEBUG [java.sql.Connection,27] - {conn-100032} Connection
2015-08-18 10:10:20,986 DEBUG [com.ibatis.sqlmap.engine.cache.CacheModel,27] - Cache ‘Vclass.vclass_cache‘: retrieved object ‘[[email protected], [email protected]]‘
2015-08-18 10:10:21,014 DEBUG [java.sql.Connection,27] - {conn-100033} Connection
2015-08-18 10:10:21,015 DEBUG [java.sql.Connection,27] - {conn-100033} Preparing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:21,015 DEBUG [java.sql.PreparedStatement,27] - {pstm-100034} Executing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:21,016 DEBUG [java.sql.PreparedStatement,27] - {pstm-100034} Parameters: [10]
2015-08-18 10:10:21,016 DEBUG [java.sql.PreparedStatement,27] - {pstm-100034} Types: [java.lang.Integer]
2015-08-18 10:10:21,025 DEBUG [java.sql.ResultSet,27] - {rset-100035} ResultSet
2015-08-18 10:10:21,027 DEBUG [java.sql.ResultSet,27] - {rset-100035} Header: [COUNT(*)]
2015-08-18 10:10:21,028 DEBUG [java.sql.ResultSet,27] - {rset-100035} Result: [0]
2015-08-18 10:10:21,058 DEBUG [java.sql.Connection,27] - {conn-100036} Connection
2015-08-18 10:10:21,060 DEBUG [java.sql.Connection,27] - {conn-100036} Preparing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:21,061 DEBUG [java.sql.PreparedStatement,27] - {pstm-100037} Executing Statement: SELECT COUNT(*) FROM vclass_enroll WHERE vclass_id=?
2015-08-18 10:10:21,061 DEBUG [java.sql.PreparedStatement,27] - {pstm-100037} Parameters: [9]
2015-08-18 10:10:21,062 DEBUG [java.sql.PreparedStatement,27] - {pstm-100037} Types: [java.lang.Integer]
2015-08-18 10:10:21,072 DEBUG [java.sql.ResultSet,27] - {rset-100038} ResultSet
2015-08-18 10:10:21,074 DEBUG [java.sql.ResultSet,27] - {rset-100038} Header: [COUNT(*)]
2015-08-18 10:10:21,075 DEBUG [java.sql.ResultSet,27] - {rset-100038} Result: [0]
2015-08-18 10:10:21,104 DEBUG [java.sql.Connection,27] - {conn-100039} Connection
2015-08-18 10:10:21,105 DEBUG [com.ibatis.sqlmap.engine.cache.CacheModel,27] - Cache ‘Vclass.vclass_cache‘: retrieved object ‘2‘
明显第一次后的查询,少了select * 语句,从缓存中获取数据。
版权声明:本文为博主原创文章,未经博主允许不得转载。