实体.xml文件中cacheModel

来源:互联网 发布:光伏智能软件 编辑:程序博客网 时间:2024/04/30 08:07
  1. cacheModel属性说明
Xml代码
  1. <cacheModel id="code-CacheModel" type ="LRU" readOnly="true" serialize="false">  
  2.     <flushInterval seconds="60"/>  
  3.     <flushOnExecute statement="CodeModel-InsertCodeModel"/>  
  4.     <property name="cache-size" value="1000" />    
  5. </cacheModel>  
<cacheModel id="code-CacheModel" type ="LRU" readOnly="true" serialize="false"><flushInterval seconds="60"/><flushOnExecute statement="CodeModel-InsertCodeModel"/><property name="cache-size" value="1000" /> </cacheModel>


id: cacheModel的id.

type: cache的类型. ibatis目前提供了LRU,MEMORY,FIFO,OSCACHE这四种.
  •       FIFO: com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController
  •       LRU:  com.ibatis.sqlmap.engine.cache.fifo.LruCacheController
  •       MEMORY: com.ibatis.sqlmap.engine.cache.fifo.MemoryCacheController
  •       OSCACHE: com.ibatis.sqlmap.engine.cache.fifo.OSCacheController


      当然,你也可以自己来实现Cache, 你需要做的是让你的Cache类 implements com.ibatis.sqlmap.engine.cache.CacheController.

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

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

flushInterval: Cache刷新间隔. 可以配置hours,minutes,seconds,milliseconds.
           注: 不是说,间隔时间到了,在Cache的statement会自己刷新,而是说,在间隔时间过了后,下次的查询
   将不会从Cache中直接去值,而会用SQL去查.也就是: 如果,间隔时间过了,还没有Cache对应的statement执行
   的话,那么Cache中就会一直是旧的,不用担心Cache数据是旧的,因为下次的查询将会直接从SQL查询,而非Cache,查询的结果也会去更新Cache的值.

flushOnExecute: 当这些statement被执行了,那么下次的查询将会通过SQL去查,同时用查询结果更新Cache.
           注: 和flushInterval的刷新一样,不是主动刷新,而是由下次查询来触发被动刷新.
               在一个cacheModel中可以指定多个flushOnExecute.

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" />
  OSCACHE: 该属性不可用, 而是依赖在ClassPath下的一个oscache.properties文件



2. 具体例子
Xml代码
  1. <sqlMap namespace="CodeModel">  
  2.     <typeAlias alias="codeModel" type="cn.iwoo.demo.model.CodeModel"/>  
  3.        
  4.     <cacheModel id="code-CacheModel" type ="LRU" readOnly="true" serialize="false">  
  5.         <flushInterval seconds="60"/>  
  6.         <flushOnExecute statement="CodeModel-InsertCodeModel"/>  
  7.         <property name="cache-size" value="1000" />    
  8.     </cacheModel>  
  9.        
  10.     <resultMap id="BaseModelResult" class="codeModel">  
  11.         <result property="createTime" column="CREATE_TIME"/>  
  12.         <result property="deleteTime" column="DELETE_TIME"/>  
  13.         <result property="defunctInd" column="DEFUNCT_IND"/>  
  14.     </resultMap>  
  15.        
  16.     <resultMap id="CodeModelResult" class="codeModel" extends="BaseModelResult">  
  17.         <result property="id" column="CODE_ID"/>  
  18.     </resultMap>  
  19.        
  20.     <select id="CodeModel-SelectCodeModel" parameterClass="java.lang.Long" resultMap="CodeModelResult" cacheModel="code-CacheModel">  
  21.         SELECT C.CODE_ID, C.CREATE_TIME, C.DELETE_TIME, C.DEFUNCT_IND    
  22.           FROM CODEMSTR C    
  23.          WHERE 11 = 1   
  24.          <isNotNull prepend="AND ">  
  25.              C.CODE_ID = #value#   
  26.          </isNotNull>  
  27.         </select>  
  28.        
  29.     <insert id="CodeModel-InsertCodeModel" parameterClass="codeModel">  
  30.         INSERT INTO CODEMSTR(CODE_ID, CREATE_TIME, DELETE_TIME, DEFUNCT_IND)   
  31.         VALUES (#id#, #createTime#, #deleteTime#, #defunctInd#)   
  32.     </insert>  
  33.        
  34. </sqlMap>  
<sqlMap namespace="CodeModel"><typeAlias alias="codeModel" type="cn.iwoo.demo.model.CodeModel"/><cacheModel id="code-CacheModel" type ="LRU" readOnly="true" serialize="false"><flushInterval seconds="60"/><flushOnExecute statement="CodeModel-InsertCodeModel"/><property name="cache-size" value="1000" /> </cacheModel><resultMap id="BaseModelResult" class="codeModel"><result property="createTime" column="CREATE_TIME"/><result property="deleteTime" column="DELETE_TIME"/><result property="defunctInd" column="DEFUNCT_IND"/></resultMap><resultMap id="CodeModelResult" class="codeModel" extends="BaseModelResult"><result property="id" column="CODE_ID"/></resultMap><select id="CodeModel-SelectCodeModel" parameterClass="java.lang.Long" resultMap="CodeModelResult" cacheModel="code-CacheModel">SELECT C.CODE_ID, C.CREATE_TIME, C.DELETE_TIME, C.DEFUNCT_IND   FROM CODEMSTR C  WHERE 1 = 1 <isNotNull prepend="AND ">     C.CODE_ID = #value# </isNotNull>        </select><insert id="CodeModel-InsertCodeModel" parameterClass="codeModel">INSERT INTO CODEMSTR(CODE_ID, CREATE_TIME, DELETE_TIME, DEFUNCT_IND)VALUES (#id#, #createTime#, #deleteTime#, #defunctInd#)</insert></sqlMap>


注: cache的key是具体执行的SQL语句.

举例来说明: 如上面的<select id="CodeModel-SelectCodeModel".

第一次值为1的查询: 通过SQL查询, 缓存的key就会是SELECT...AND C.CODE_ID = 1, 缓存的value就是查询结果.
再执行值为1的查询: 结果就会直接从cache中取. [当然前提必须是:flushInterval还没到,同时flushOnExecute对应的statement还没有被执行过]

如果再执行一次值为2的查询: 那么由于key为 SELECT...AND C.CODE_ID = 2, 在cache不存在, 就会直接执行SQL, 并将结果缓存起来.

也就是cache完全是以具体执行的SQL语句作为key来存储的.