Ibatis.Net 学习手记二 缓存

Ibatis.Net 学习手记二 缓存

 

Ibatis.Net 的缓存策略有几种:MEMORYLRU、FIFO

 MEMORY
     MEMORY高速缓存是一种基于引用的高速缓存。有了STRONG、SOFT、WEAK这三种引用类型,
 LRU
     LRU类型的高速缓存模型使用最近最少使用策略来管理高速缓存。该高速缓存的内部机制会在后台记录哪些对象最近最少使用,一旦超过高速缓存大小限制就会废弃它们。
 FIFO
     FIFO高速缓存模型采用先进先出的管理策略,是一种基于时间的策略。

 缓存的配置方式:

SqlMap.config

  <settings>
    <setting cacheModelsEnabled="true"/>
    <setting useStatementNamespaces="false" />

   </settings>  

 这里的设置是默认的 cacheModelsEnabled表示是否开启缓存

 <cacheModels>
    <cacheModel id="personcache" implementation="MEMORY" readOnly="true" serialize="false">
      <flushInterval hours="24"/>
      <flushOnExecute  statement="UpdatePerson"/>
      <flushOnExecute  statement="InsertPerson"/>
      <property name="reference-type" value="Strong" />
    </cacheModel>
  </cacheModels>

 

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返回对象的副本

 

调用方式:

     <select id="SelectAll" resultMap="SelectAllResult" cacheModel="personcache">  

      select
      Id,Name,Password
      from Person
    </select


 

昨天看了下Ibatis有几种缓存形式。也测试了下需要缓存是可以的不过 flushOnExecute 更新方面感觉有点问题...

posted @ 2011-12-07 11:51  Nopcn  阅读(319)  评论(0编辑  收藏  举报