- <select id="GetCachedAccountsViaResultMap"
- resultMap="account-result"
- cacheModel="account-cache" >
- select *
- from Accounts
- order by Account_ID
- </select>
-
这里 需要注意的几点:select * from Accounts order by Account_ID 是SQL语句。
最主要的就是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 >
< 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"是设置缓存的实现方式,可以指定LRU、FIFO等,有点类似于内存的页替换策略。MEMORY是最常使用的一种方式。
flushOnExecute设置的是当执行了这些语句时更新缓存,上述是存储过程。
配置好之后我进行了一个简单的测试,基本上是可以的,但也有一点问题:
1、第一次查询结果是4条记录,当我手工往数据库中插入一条记录时,第二次查询还是4条记录
2、当我把系统时间改成第二天(24小时后),再查,得到的结果是5条记录
3、当我执行了InsertAccountViaParameterMap语句插入一条记录时,再查询得到的是6条记录
也就是说:当系统中的表从不进行手工维护,也不由第三方程序修改时,可以使用数据库缓存的方式提高效率