iBATIS.net數(shù)據(jù)庫緩存模式淺析
在iBATIS.net中提供了數(shù)據(jù)庫緩存的模式,可以提高訪問效率。對于一些不常更新的表可以直接利用iBATIS.net的緩存方式。
要使用iBATIS.net的數(shù)據(jù)庫緩存,只要利用配置文件就可以了,實現(xiàn)起來比較簡單:
- ﹤select id="GetCachedAccountsViaResultMap"
- resultMap="account-result"
- cacheModel="account-cache" ﹥
- select *
- from Accounts
- order by Account_ID
- ﹤/select﹥
最主要的就是cacheModel="account-cache",指定緩存的方式,如下,是具體配置緩存的地方:
- ﹤cacheModels﹥
- ﹤cacheModel id="account-cache" implementation="MEMORY" ﹥
- ﹤flushInterval hours="24"/﹥
- ﹤flushOnExecute statement="UpdateAccountViaInlineParameters"/﹥
- ﹤flushOnExecute statement="UpdateAccountViaParameterMap"/﹥
- ﹤flushOnExecute statement="InsertAccountViaParameterMap"/﹥
- ﹤property name="Type" value="Weak"/﹥
- ﹤/cacheModel﹥
- ﹤/cacheModels﹥
其中:implementation="MEMORY"是設置緩存的實現(xiàn)方式,可以指定LRU、FIFO等,有點類似于內(nèi)存的頁替換策略。MEMORY是最常使用的一種方式。
flushOnExecute設置的是當執(zhí)行了這些語句時更新緩存。
配置好之后我進行了一個簡單的測試,基本上是可以的,但也有一點問題:
1、***次查詢結果是4條記錄,當我手工往數(shù)據(jù)庫中插入一條記錄時,第二次查詢還是4條記錄
2、當我把系統(tǒng)時間改成第二天(24小時后),再查,得到的結果是5條記錄
3、當我執(zhí)行了InsertAccountViaParameterMap語句插入一條記錄時,再查詢得到的是6條記錄
也就是說:當系統(tǒng)中的表從不進行手工維護,也不由第三方程序修改時,可以使用數(shù)據(jù)庫緩存的方式提高效率。
以上就是iBATIS.net數(shù)據(jù)庫緩存模式的介紹,是不是對你了解iBATIS.net數(shù)據(jù)庫緩存模式有所幫助呢?
【編輯推薦】