IBatis.net 缓存
首先在sqlmap.config中启用缓存
<setting cacheModelsEnabled="true"/>
然后新建一个xml:CacheAccount.xml
并在sqlmap.config中的sqlMaps节点配置
<sqlMaps>
<sqlMap resource="Maps/Account.xml"/>
<sqlMap resource="Maps/CacheAccount.xml"/>
</sqlMaps>
新建立的CacheAccount.xml如下
<?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>
说明:
Ibatis.Net 的缓存策略有几种:MEMORY、LRU、FIFO
MEMORY高速缓存是一种基于引用的高速缓存。有了STRONG、SOFT、WEAK这三种引用类型,
LRU
LRU类型的高速缓存模型使用最近最少使用策略来管理高速缓存。该高速缓存的内部机制会在后台记录哪些对象最近最少使用,一旦超过高速缓存大小限制就会废弃它们。
FIFO
FIFO高速缓存模型采用先进先出的管理策略,是一种基于时间的策略。
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"

浙公网安备 33010602011771号