ibatis学习笔记(四) 缓存

原创 2012年03月21日 17:01:06

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.] |



iBATIS系统学习笔记四

学习笔记四,实践技巧

yii2数据缓存 学习笔记

  • 2016年04月18日 11:20
  • 11KB
  • 下载

redis 缓存技术学习笔记

  • 2015年07月23日 09:40
  • 147KB
  • 下载

《Spring技术内幕》学习笔记13——SqlMapClientTemplate对Ibatis的封装

1. SqlMapClientFactoryBean: Spring中通过SqlMapClientTemplate提供对Ibatis的支持,与Spring对Hibernate的支持类似,Spring...
  • lcczzu
  • lcczzu
  • 2015年07月16日 10:57
  • 310

iBATIS学习笔记(一)查询记录

  • 2012年12月08日 20:03
  • 72KB
  • 下载

iBatis2学习笔记

  • 2009年02月08日 21:20
  • 135KB
  • 下载

《Spring技术内幕》学习笔记13——SqlMapClientTemplate对Ibatis的封装

1. SqlMapClientFactoryBean: Spring中通过SqlMapClientTemplate提供对Ibatis的支持,与Spring对Hibernate的支持类似,Spring...
  • chjttony
  • chjttony
  • 2011年05月14日 16:36
  • 11271

ibatis学习笔记.txt

  • 2011年03月11日 14:53
  • 5KB
  • 下载

持久层框架ibatis学习笔记

  • 2011年06月15日 15:30
  • 417KB
  • 下载

ibatis学习笔记(二)>>>>>>>sqlMapConfig.xml文件详解

ibatis基础代码包括: 1.    ibatis实例配置 一个典型的配置文件如下(具体配置项目的含义见后):   ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ibatis学习笔记(四) 缓存
举报原因:
原因补充:

(最多只允许输入30个字)