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
  • 下载

Ibatis学习笔记

Ibatis学习笔记 最近学习了ibatis连接数据库   搭建iBatis 开发环境: 仅仅需要两个包:数据库驱动包,和ibatis包。 三个配置文件:数据库连接属性文件、总配置文件、实体的映射文件...

IBATIS学习笔记

  • 2013-06-16 17:08
  • 347KB
  • 下载

ibatis_3_学习笔记.pdf

  • 2012-12-08 20:40
  • 249KB
  • 下载

电商平台学习笔记(三)——Mybatis启动异常org.apache.ibatis.type.TypeException

今天一大早刚过来写代码,接着昨天使用MapperScannerConfigurer来扫包!以帮助从DAO层解脱出来(只需要定义接口)!结果就遇到了Bug。 异常内容如下: java.lan...

iBATIS学习笔记

  • 2012-09-19 12:35
  • 347KB
  • 下载

iBATIS系统学习笔记二

基础知识,涉及到安装和配置,已映射语句,非查询语句,高级查询技术,事务,动态SQL
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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