IBatisNet1.5 -- 映射文件--Cache Models

         Cache也是经常讨论的一个话题之一,在我们系统开发的过程中,总会存在着这样一类数据,它们更新频率很低,然而使用的频率却非常之高。为了提高系统性能,我们通常将此类数据装入缓存。Ibatisnet 也有自己的缓存系统。
        MappedStatement的查询结果集可以根据cacheModel的值来确定它是否被装入缓存以及如何装入缓存。而Cache Model也是在配置文件中事先定义好的。具体的定义方式如下:
< cacheModel  id ="employee-cache"  implementation ="LRU"  readOnly ="true"  serialize ="false" >
  
< flushInterval  hours ="24" />
  
< flushOnExecute   statement ="insertEmployee" />
  
< flushOnExecute   statement ="updateEmployee" />
  
< flushOnExecute   statement ="deleteEmployee" />
  
< property  name ="CacheSize"  value ="100" />
</ cacheModel >

上面的cache model配置好之后,它会创建一个名为"employee-cache"的缓存实例,implementation表示它的实现规则是LRU,即: Leaste Recently Used。ibatisnet还有MEMORY,FIFO几种缓存的模式。readOnly的值说明缓存是只读的还是可读写的,如果readOnly为 true,缓存就是只读,false为可读写。serialize指示缓存是在整个Application中有效还是仅作用于当前的Session。 flushInterval的值表示缓存的有效期,上面的配置中则表示改缓存每24小时清空一次,当然有效期还可以用minutes, seconds or milliseconds来表示。flushOnExecute表示缓存将在执行了什么操作时被清空,上面的配置中表示该缓存在执行 insertEmployee or updateEmployee or deleteEmployee时被清空。CacheSize表示缓存的最大条目,当超过这个值的时候就会按照implementation中定义的规则来 清除相应的条目,上面配置中则表示当缓存的条目超过100时,则移出最近最少使用的条目。当我们配置好cacheModel之后就可以在 statement中使用了,如下:

< statement  id ="EmployeeList"  cacheModel ="employee-cache" >
  select * from Employee
</ statement >

你可能感兴趣的:(ibatis)