ibatis学习笔记(四) 缓存
1.缓存配置
首先我们看一下缓存配置的样式<cacheModel id="cache1" type="LRU" readOnly="true" serialize="false">
<property name="cache-size" value="1000" />
</cacheModel>
<select id="getProduct" parameterClass="java.lang.Integer"
resultClass="product" cacheModel="cache1">
<![CDATA[
select * from t_product where id = #value#
]]>
</select>
cacheModel主要有下面几个配置点:
flushInterval :
设定缓存有效期,如果超过此设定值,则将此CacheModel的缓存清空。
size:
本CacheModel中最大容纳的数据对象数量。
flushOnExecute:
指定执行特定Statement时,将缓存清空。如updateUser操作将更
新数据库中的用户信息,这将导致缓存中的数据对象与数据库中的实际
数据发生偏差,因此必须将缓存清空以避免脏数据的出现。
type:
其中 type分为:
1.MEMORY
2.LRU
3.FIFO
4.OSCACHE
readOnly:
readOnly值的是缓存中的数据对象是否只读。这里的只读并不是意味着数据对象一
旦放入缓存中就无法再对数据进行修改。而是当数据对象发生变化的时候,如数据对
象的某个属性发生了变化,则此数据对象就将被从缓存中废除,下次需要重新从数据
库读取数据,构造新的数据对象。
而 readOnly="false"则意味着缓存中的数据对象可更新,如user 对象的name
属性发生改变。
serialize:
如果需要全局的数据缓存,CacheModel的serialize属性必须被设为true。否则数据缓存只对当前Session(可简单理解为当前线程)有效,局部缓存对系统的整体性能提
升有限。
在 serialize="true"的情况下,如果有多个Session同时从Cache 中读取某个数据对象,Cache 将为每个Session返回一个对象的复本,也就是说,每个Session 将得到包含相同信息的不同对象实例。因而Session 可以对其从Cache 获得的数据进行存取而无需担心多线程并发情况下的同步冲突
2.测试
我们调用如下方法:
Product prod = (Product)sqlMap.queryForObject("getProduct", 1);
prod = (Product)sqlMap.queryForObject("getProduct", 1);
然后我们查看测试结果,如果不配cache时,结果是这样的:
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Created connection 324473687.] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{conn-100000} Connection] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} PreparedStatement: select * from t_product where id = ? ] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Parameters: [1]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Types: [java.lang.Integer]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} ResultSet] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Header: [id, name, description, url, price]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Result: [1, ?? T400 ???, null, null, 8999.0]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Returned connection 324473687 to pool.] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Checked out connection 324473687 from pool.] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{conn-100003} Connection] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100004} PreparedStatement: select * from t_product where id = ? ] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100004} Parameters: [1]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100004} Types: [java.lang.Integer]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100005} ResultSet] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100005} Header: [id, name, description, url, price]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100005} Result: [1, ?? T400 ???, null, null, 8999.0]] |
[DEBUG] [2012-03-21 16:57:42] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Returned connection 324473687 to pool.] |
会执行两边sql,
如果配置cache的话,只会执行一次
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Created connection 423369020.] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{conn-100000} Connection] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} PreparedStatement: select * from t_product where id = ? ] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Parameters: [1]] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{pstm-100001} Types: [java.lang.Integer]] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} ResultSet] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Header: [id, name, description, url, price]] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [{rset-100002} Result: [1, ?? T400 ???, null, null, 8999.0]] |
[DEBUG] [2012-03-21 17:03:24] [Thread = main | Class = com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl | Method = debug | Line = 23 ] | [Returned connection 423369020 to pool.] |