ibatis提供四种缓存方案,LRU、FIFO、MEMORY、EHCACHE,通过定义不同的cacheModel,将数据缓存在cache中。
具体写法参考如下
<cacheModel id="cache-name" type="LRU" readOnly="true" serialize="false">
<property name="cache-size" value="1000" />
</cacheModel>
<select id="getBean" resultMap="bean" cacheModel="cache-name">
<![CDATA[select * from table]]>
</select>
一、参见com.ibatis.sqlmap.engine.builder.xml.SqlMapParser
parser.addNodelet("/sqlMap/cacheModel", new Nodelet() {
public void process(Node node) throws Exception {
vars.currentCacheModel = new CacheModel();
vars.currentProperties = new Properties();
}
});
parse在解析xml的时候,遇到一个cacheModel标签,则创建一个CacheModel的对象
vars.currentCacheModel.setControllerClassName(type);
if (vars.client.getDelegate().isCacheModelsEnabled()) {
vars.client.getDelegate().addCacheModel(vars.currentCacheModel);
}
并通过如上方法,成功的将cacheModel对象加入到map中缓存起来,同时为cacheModel设置缓存控制器(LRU、FIFO、MEMORY、EHCACHE),实现不同的缓存方案
因为是单例模式, SqlMapExecutorDelegate通过HashMap cacheModels ,持有了全部的cacheModel,并作为静态对象全局共享。
二、参见com.ibatis.sqlmap.engine.builder.xml.SqlStatementParser
解析statement时,如果cache可用,获取到对应的cacheModel,执行相关的缓存操作
if (cacheModelName != null && cacheModelName.length() > 0 && vars.client.getDelegate().isCacheModelsEnabled()) {
CacheModel cacheModel = vars.client.getDelegate().getCacheModel(cacheModelName);
return new CachingStatement(statement, cacheModel);
}
三、参见com.ibatis.sqlmap.engine.mapping.statement.CachingStatement
CachingStatement封装了很多query的方法,以executeQueryForObject为例
CacheKey cacheKey = getCacheKey(request, parameterObject);
cacheKey.update("executeQueryForObject");
Object object = cacheModel.getObject(cacheKey);
if (object == CacheModel.NULL_OBJECT){
// This was cached, but null
object = null;
}else if (object == null) {
object = statement.executeQueryForObject(request, trans, parameterObject, resultObject);
cacheModel.putObject(cacheKey, object);
}
return object;
如果没从cacheModel中获取到缓存数据,则继续查询,并将查询结果放到缓存中。
从这里的代码也能看到,cacheModel是允许存在缓存对象为null的情况,因此如果查询结果为null,也会缓存起来,这里要稍加注意。
四、参加com.ibatis.sqlmap.engine.cache.CacheModel
cacheModel.getObject(cacheKey)的方法,由cacheMode定义的cacheController实现(LRU、FIFO、MEMORY、EHCACHE),cacheModel作为单例类的成员变量,可以全局共享,同样CacheController做为cacheModel的成员变量,也可以全局共享,这样CacheController持有的Map cache和List keyList,也在cacheModel可以控制的范围内全局共享了,这样就实现了缓存的效果。
这里有个有意思的地方,由于cacheModel在getObject的时候,会自动统计request和hit的次数,因此使用缓存的方式,cacheModel可以提供输出缓存命中率的方法 -- 参见 public double getHitRatio()
分享到:
相关推荐
Struts2 Spring iBATIS 架构 实现 Struts2 Spring iBATIS 架构 实现 Struts2 Spring iBATIS 架构 实现 Struts2 Spring iBATIS 架构 实现
自己写的ibatis 框架,跟ibatis功能差不多
ibatis实现原理解析
Ibatis和Spring整合例子,实现增删改查功能.
IbatisDemo实现基本的CRUD操作
ibatis实现分页技术,其中的分页都已经封装好了,大家需要的话可以下载下来看下
ibatis实现CRUD操作,它比学好hibernate更简单
ibatis学习 ibatis总结 ibatis ibatis ibatis
通过修改ibatis源码实现sql分页。由于ibatis默认分页是全部查出,然后再从中取出相应的数据实现分页,效率底。所以需要扩展
实现ibatis框架对student表的增删改查(包括模糊查询和用序列自动生成主键)
ibatis缓存技术详解,详细讲述了ibaits缓存应用于实现。
struts2+spring+ibatis+oracle+分页搜索+上传附件实例!完整版!
一个ibatis实现增删改查功能demo。里面实现的增删改查,而且解决了ibatis不能多参数传递的缺点。可以实现多参数传递。
实现ibatis对数据的连接,增查删改。.xml文件对sql语句的映射。基本知识基本全部覆盖
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
ssi项目实例,采用struts2.18、spring2.5、ibatis2.3实现
iBATIS教程是iBATIS初学者需要掌握什么内容呢?那么这篇文章就会告诉你。
ibatis的实现(包含模糊查询、关联查询、增删改查) 数据库sql server 2005
struts2+spring+ibatis集成实现赠删改查 代码简练 高效 适合初学者 一看你就懂 继续是需要分享的 希望能帮到你
ibatis demo,ibatis例子,ibatis示例