IBatisNet 缓存使用

时间:2021-03-15 23:48:29

参考:http://www.cnblogs.com/xiaogangqq123/archive/2011/06/30/2094905.html

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="CacheAccount" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!--
implementation="MEMORY"是设置缓存的实现方式,可以指定LRU、FIFO等,有点类似于内存的页替换策略。MEMORY是最常使用的一种方式。
flushOnExecute设置的是当执行了这些语句时更新缓存 readOnly="false" serialize="true"
-->
<cacheModels>
<cacheModel id="account-cache" implementation="MEMORY" >
<flushInterval hours="24"/>
<flushOnExecute statement="Account.sql_InsertOne"/>
<!--<flushOnExecute statement="sql_Delete"/>-->
<flushOnExecute statement="CacheAccount.sql_update"/>
<property name="Type" value="Weak"/>
</cacheModel>
</cacheModels>
<resultMaps>
<resultMap id="Account-result" class="Account">
<result property="Id" column="id"/>
<result property="Item" column="Item"/>
<result property="Year" column="Year"/>
<result property="Month" column="Month"/>
<result property="Day" column="Day"/>
<result property="CreateOn" column="CreateOn"/>
<result property="Level" column="Level"/>
</resultMap>
</resultMaps> <statements>
<select id="sql_CacheSelect" resultMap="Account-result" cacheModel="account-cache" >
select * from Accounts
</select> <delete id="sql_Delete" parameterClass="int">
delete from Accounts
<dynamic prepend="where">
<isParameterPresent property="id" prepend="">
[id] = #id#
</isParameterPresent>
</dynamic>
</delete> <update id="sql_update" parameterClass="Account">
update Accounts set Item = #Item#
where id = #Id#
</update>
</statements>
</sqlMap>

cacheModels节点是配置缓存的节点,cacheModel的property元素来设置,目前包括以下的 4 个实现:

MEMORY” (com.ibatis.db.sqlmap.cache.memory.MemoryCacheController) 。MEMORY cache 实现使用 reference 类型来管理 cache 的行为。垃圾收集器可以根据 reference类型判断是否要回收 cache 中的数据。MEMORY实现适用于没有统一的对象重用模式的应用,或内存不足的应用。

LRU” (com.ibatis.db.sqlmap.cache.lru.LruCacheController) 。LRU Cache 实现用“近期最少使用”原则来确定如何从 Cache 中清除对象。当 Cache溢出时,最近最少使用的对象将被从 Cache 中清除。使用这种方法,如果一个特定的对象总是被使用,它将保留在 Cache 中,而且被清除的可能性最小。对于在较长的期间内,某些用户经常使用某些特定对象的情况(例如,在 PaginatedList 和常用的查询关键字结果集中翻页) ,LRU Cache 是一个不错的选择。

FIFO” (com.ibatis.db.sqlmap.cache.fifo.FifoCacheController) 。FIFO Cache 实现用“先进先出”原则来确定如何从 Cache 中清除对象。当 Cache 溢出时,最先进入 Cache 的对象将从 Cache 中清除。对于短时间内持续引用特定的查询而后很可能不再使用的情况,FIFO Cache 是很好的选择。

OSCACHE” (com.ibatis.db.sqlmap.cache.oscache.OSCacheController)  。OSCACHE Cache 实现是OSCache2.0缓存引擎的一个 Plugin。它具有高度的可配置性,分布式,高度的灵活性。

flushOnExecute:设置的是当执行了这些语句时更新缓存。

flushInterval : Cache刷新间隔. 可以配置hours,minutes,seconds,milliseconds.

property : 这是针对cacheModel的额外的一些属性配置.不同type的cacheModel将会有自己专有的一些property配置. 
                 FIFO: <property name="size" value="100" /> 
                LRU: <property name="cache-size" value="100" /> 
                MEMORY: <property name="reference-type" value="WEAK" />

readOnly : 是否只读. 默认为true, 只读.

serialize : 是否从Cache中读取同一个对象,还是对象的副本. 只有在readOnly=false才有效.  因为Cache是只读的,那么为不同session返回的对象肯定是一个. 只有在Cache是可读写的时候,才需要为每个session返回对象的副本.

注意我这里更改了命名空间:namespace="CacheAccount"

select 节点 加入 cacheModel="account-cache"

结论以及注意要点:如:

implementation="MEMORY"   这边设置了这样,就要 在 property 加上 MEMORY: <property name="reference-type" value="WEAK" />
implementation=“LRU”      <property name="cache-size" value="100" /> 

否则无效果

该缓存机制总体缺陷,对于经常变的 <select 数据集只会缓存最后一次查询的结果,一旦 <select 的SQL数据集变更如关键字查询,那么缓存就失去意义了。