cacheModel的屬性值等于指定的 cacheModel 元素的 name 屬性值。屬性 cacheModel 定義查詢 mapped statement 的緩存。每一個查詢 mapped statement 可以使用不同或相同的cacheModel,以下給出個例子。
<cacheModel id="product-cache" imlementation="LRU"(或 type ="LRU") [readOnly=“true” serialize=“false”]> // [ ] 表示可選
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name="size" value="1000" />
</cacheModel>
<statement id="getPoductList" parameerClass="int" cacheModel="product-cache">
select * from PRODUCT where PRD_CAT_ID = #value#
</statement>
// 使用“近期最少使用”(LRU)實現(xiàn)
// 上面例子中,“getProductList”的緩存每 24 小時刷新一次,或當(dāng)更新的操作發(fā)生時刷新。一個 cacheModel 只能有一個 flushInteval 元素,它可以使用 hours,minutes,seconds或 milliseconds 來設(shè)定。某些 cache model 的實現(xiàn)可能需要另外的屬性,如上面的“cache-size”屬性。在 LRU cache model 中,cache-size指定了緩存儲存的項數(shù)
緩存類型
Cache Model 使用插件方式來支持不同的緩存算法。它的實現(xiàn)在cacheModel 的用 type 屬性來指定(如上所示)。指定的實現(xiàn)類必須實現(xiàn) CacheController接口,或是下面 4個別名中的其中之一。Cache Model 實現(xiàn)的其他配置參數(shù)通過 cacheModel的 property元素來設(shè)置。目前包括以下的 4 個實現(xiàn):
"MEMORY" (com.ibatis.db.sqlmap.cache.memory.MemoryCacheController)
MEMORY cache 實現(xiàn)使用 reference 類型來管理 cache 的行為。垃圾收集器可以根據(jù) reference類型判斷是否要回收 cache 中的數(shù)據(jù)。MEMORY實現(xiàn)適用于沒有統(tǒng)一的對象重用模式的應(yīng)用,或內(nèi)存不足的應(yīng)用。
MEMORY實現(xiàn)可以這樣配置:
<cacheModel id="product-cache" type="MEMORY">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name="reference-type" value="WEAK"/>
</cacheModel>
// MEMORY cache 實現(xiàn)只認(rèn)識一個<property>元素。這個名為“reference-type”屬性的值必須是 STRONG,SOFT 和 WEAK 三者其一。這三個值分別對應(yīng)于 JVM 不同的內(nèi)存 reference類型。
"LRU" (com.ibatis.db.sqlmapache.lru.LruCacheController)
LRU Cache 實現(xiàn)用“近期最少使用”原則來確定如何從 Cache 中清除對象。當(dāng) Cache 溢出時,最近最少使用的對象將被從 Cache 中清除。使用這種方法,如果一個特定的對象總是被使用,它將保留在 Cache 中,而且被清除的可能性最小。對于在較長的期間內(nèi),某些用戶經(jīng)常使用某些特定對象的情況(例如,在 PaginatedList 和常用的查詢關(guān)鍵字結(jié)果集中翻
頁),LRU Cache 是一個不錯的選擇。
LRU Cache實現(xiàn)可以這樣配置:
<cacheModel id="product-cache" type="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name="size" value="1000"/>
</cacheModel>
// LRU Cache實現(xiàn)只認(rèn)可一個 property元素。其名為“cache-size”的屬性值必須是整數(shù),代表同時保存在 Cache中對象的最大數(shù)目。值得注意的是,這里指的對象可以是任意的,從單一的 String 對象到 Java Bean 的 ArrayList 對象都可以。因此,不要 Cache太多的對象,以免內(nèi)存不足。
"FIFO" (com.ibatis.db.sqlmap.cache.fifo.FifoCacheController)
FIFO Cache 實現(xiàn)用“先進先出”原則來確定如何從 Cache 中清除對象。當(dāng) Cache 溢出時,最先進入 Cache 的對象將從 Cache 中清除。對于短時間內(nèi)持續(xù)引用特定的查詢而后很可能不再使用的情況,F(xiàn)IFO Cache 是很好的選擇。
FIFO Cache可以這樣配置:
<cacheModel id="product-cache" type="FIFO">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name="size" value="1000"/>
</cacheModel>
// property 的 size 同LRU
"OSCACHE" (com.ibatis.db.sqlmap.cache.oscache.OSCacheController)
OSCACHE Cache 實現(xiàn)是OSCache2.0緩存引擎的一個 Plugin。它具有高度的可配置性,
分布式,高度的靈活性。
OSCACHE 實現(xiàn)可以這樣配置:
<cacheModel id="product-cache" type="OSCACHE">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
</cacheModel>
// OSCACHE 實現(xiàn)不使用 property 元素,而是在類路徑的根路徑中使用標(biāo)準(zhǔn)的oscache.properties 文件進行配置。在 oscache.properties文件中,您可以配置 Cache 的算法(和上面討論的算法很類似),Cache 的大小,持久化方法(內(nèi)存,文件等)和集群方法。
要獲得更詳細(xì)的信息,請參考 OSCache 文檔。OSCache 及其文檔可以從 OpenSymphony
網(wǎng)站上獲取:
http://www.opensymphony.com/oscache/