iBATIS緩存cacheModel屬性淺析
iBATIS緩存模型可以在Mapped Statement中使用,在內(nèi)存中緩存常用的數(shù)據(jù)。屬性cacheModel 定義查詢mapped statement 的緩存。每一個(gè)查詢mapped statement 可以使用不同或相同的cacheModel。以下給出個(gè)例子:
- ﹤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 小時(shí)刷新一次,或當(dāng)更新的操作發(fā)生時(shí)刷新。
Cache Model使用插件方式來(lái)支持不同的緩存算法。它的實(shí)現(xiàn)在 cacheModel 的用 type屬性來(lái)指定(如上所示)。指定的實(shí)現(xiàn)類必須實(shí)現(xiàn) CacheController接口,或是下面 4個(gè)別名中的其中之一。Cache Model 實(shí)現(xiàn)的其他配置參數(shù)通過(guò) cacheModel的 property元素來(lái)設(shè)置。目前包括以下的 4 個(gè)實(shí)現(xiàn):
◆"MEMORY” (com.ibatis.db.sqlmap.cache.memory.MemoryCacheController) 。MEMORY cache 實(shí)現(xiàn)使用 reference 類型來(lái)管理 cache 的行為。垃圾收集器可以根據(jù) reference類型判斷是否要回收 cache 中的數(shù)據(jù)。MEMORY實(shí)現(xiàn)適用于沒(méi)有統(tǒng)一的對(duì)象重用模式的應(yīng)用,或內(nèi)存不足的應(yīng)用。
◆“LRU” (com.ibatis.db.sqlmap.cache.lru.LruCacheController) 。LRU Cache 實(shí)現(xiàn)用“近期最少使用”原則來(lái)確定如何從 Cache 中清除對(duì)象。當(dāng) Cache溢出時(shí),最近最少使用的對(duì)象將被從 Cache 中清除。使用這種方法,如果一個(gè)特定的對(duì)象總是被使用,它將保留在 Cache 中,而且被清除的可能性最小。對(duì)于在較長(zhǎng)的期間內(nèi),某些用戶經(jīng)常使用某些特定對(duì)象的情況(例如,在 PaginatedList 和常用的查詢關(guān)鍵字結(jié)果集中翻頁(yè)) ,LRU Cache 是一個(gè)不錯(cuò)的選擇。
◆“FIFO” (com.ibatis.db.sqlmap.cache.fifo.FifoCacheController) 。FIFO Cache 實(shí)現(xiàn)用“先進(jìn)先出”原則來(lái)確定如何從 Cache 中清除對(duì)象。當(dāng) Cache 溢出時(shí),***進(jìn)入 Cache 的對(duì)象將從 Cache 中清除。對(duì)于短時(shí)間內(nèi)持續(xù)引用特定的查詢而后很可能不再使用的情況,F(xiàn)IFO Cache 是很好的選擇。
◆“OSCACHE” (com.ibatis.db.sqlmap.cache.oscache.OSCacheController) 。OSCACHE Cache 實(shí)現(xiàn)是OSCache2.0緩存引擎的一個(gè) Plugin。它具有高度的可配置性,分布式,高度的靈活性。
iBATIS緩存cacheModel屬性的情況就向你介紹到這里,是不是對(duì)于iBATIS緩存方面的應(yīng)用有所了解了呢?
【編輯推薦】