iBATIS緩存的使用方法--摘自iBATIS官方文檔

iBATIS可以在Mapped Statement中使用緩存模型,在內存中緩存常用的數據。屬性 cacheModel 定義查詢 mapped statement 的緩存。每一個查詢 mapped statement 可以使用不同或相同的cacheModel。以下給出個例子:

<cacheModel id="product-cache" imlementation="LRU">
 <flushInterval hours="24"/>
 <flushOnExecute statement="insertProduct"/>
 <flushOnExecute statement="updateProduct"/>
 <flushOnExecute statement="deleteProduct"/>
 <property name="size" value="1000" />
</cacheModel>
<statement id="getProductList" parameterClass="int" cacheModel="product-cache">
  select * from PRODUCT where PRD_CAT_ID = #value#
</statement>

 

上面例子中, “getProductList”的緩存使用 WEAK 引用類型,每 24 小時刷新一次,或當更新的操作發生時刷新。  
Cache Model 使用插件方式來支持不同的緩存算法。它的實現在 cacheModel 的用 type屬性來指定(如上所示)。指定的實現類必須實現 CacheController接口,或是下面 4個別名中的其中之一。Cache Model 實現的其他配置參數通過 cacheModel的 property元素來設置。目前包括以下的 4 個實現:

  1.  "MEMORY” (com.ibatis.db.sqlmap.cache.memory.MemoryCacheController) 。MEMORY cache 實現使用 reference 類型來管理 cache 的行爲。垃圾收集器可以根據 reference類型判斷是否要回收 cache 中的數據。MEMORY實現適用於沒有統一的對象重用模式的應用,或內存不足的應用。
  2. “LRU” (com.ibatis.db.sqlmap.cache.lru.LruCacheController) 。LRU Cache 實現用“近期最少使用”原則來確定如何從 Cache 中清除對象。當 Cache溢出時,最近最少使用的對象將被從 Cache 中清除。使用這種方法,如果一個特定的對象總是被使用,它將保留在 Cache 中,而且被清除的可能性最小。對於在較長的期間內,某些用戶經常使用某些特定對象的情況(例如,在 PaginatedList 和常用的查詢關鍵字結果集中翻頁) ,LRU Cache 是一個不錯的選擇。   
  3. “FIFO” (com.ibatis.db.sqlmap.cache.fifo.FifoCacheController) 。FIFO Cache 實現用“先進先出”原則來確定如何從 Cache 中清除對象。當 Cache 溢出時,最先進入 Cache 的對象將從 Cache 中清除。對於短時間內持續引用特定的查詢而後很可能不再使用的情況,FIFO Cache 是很好的選擇。 
  4.  “OSCACHE” (com.ibatis.db.sqlmap.cache.oscache.OSCacheController)  。OSCACHE Cache 實現是OSCache2.0緩存引擎的一個 Plugin。它具有高度的可配置性,分佈式,高度的靈活性。
發佈了60 篇原創文章 · 獲贊 16 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章