---
title: "iBATIS CacheModel使用浅析"
author: "af8991"
date: "2010-11-29T21:41:34Z"
tags: [".net", "Cache", "iBATIS"]
categories: ["企业架构"]
slug: "ibatis-cachemodel"
draft: false
lastmod: "2010-11-29T21:47:03Z"
id: 828652
url: "https://af8991.iteye.com/blog/828652"
---

- - # iBATIS CacheModel使用浅析
  
  <!--THE END-->
  
  - iBATIS CacheModel缓存系统是什么情况呢？那么对于iBATIS CacheModel的学习需要了解和注意什么呢？那么本文就向你介绍iBATIS CacheModel的相关信息。
  
  <!--THE END-->
  
  - iBATIS CacheModel的使用是什么情况呢？让我们首先从现状入手，Cache也是经常讨论的一个话题之一，在我们系统开发的过程中，总会存在着这样一类数据，它们更新频率很低，然而使用的频率却非常之高。为了提高系统性能，我们通常将此类数据装入缓存。iBATIS.NET也有自己的缓存系统。
    
    MappedStatement的查询结果集可以根据CacheModel的值来确定它是否被装入缓存以及如何装入缓存。而iBATIS CacheModel也是在配置文件中事先定义好的。具体的定义方式如下：
  
  ```
  
  ﹤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﹥ 
  ```
  
  iBATIS CacheModel使用浅析配置好之后，它会创建一个名为"employee-cache"的缓存实例，implementation表示它的实现规则是LRU,即：Leaste Recently Used。iBATIS.NET还有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 CacheModel的相关情况就介绍到这里，希望对你了解iBATIS CacheModel缓存系统有所帮助。
  
  转自:[http://developer.51cto.com/art/200907/138537.htm](http://developer.51cto.com/art/200907/138537.htm)