缓存初解(四)---Ibatis的缓存配置+Ehcache
项目完结,整理一些技术方面的相关收获。
已经记不得EhCacheController这个实现类最早来自于那里了,总之稍加修改后非常有效果,大家就这么用了,感谢最初开源的那位兄弟。这里,主要是做个记录,为以后类似扩展(譬如Memcached)做个准备。
iBatis提供CacheController接口,用于实现第三方缓存架构的扩展。
这里以iBatis 2.3.0,EhCache 1.2.3版本为基础,构建iBatis+EhCache实现。
EhCacheController类:
package com.ibatis.sqlmap.engine.cache.ehcache; import java.net.URL;
import java.util.Properties; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element; import com.ibatis.sqlmap.engine.cache.CacheController;
import com.ibatis.sqlmap.engine.cache.CacheModel; /**
* EhCache Implementation of the
* {@link com.ibatis.sqlmap.engine.cache.CacheController} interface to be able
* to use EhCache as a cache implementation in iBatis. You can configure your
* cache model as follows, by example, in your sqlMapping files:
*
* <pre>
* <code>
* <cacheModel id="myCache" readOnly="true" serialize="false"
* type="com.ibatis.sqlmap.engine.cache.EhCacheController" >
* <property name="configLocation"
* value="/path-to-ehcache.xml"/>
* </cacheModel> </code>
* </pre>
*
* Alternatively, you can use a type alias in your type attribute and defining
* the class with a <code><typeAlias></code> declaration:
*
* <pre>
* <code>
* <sqlMapConfig>
* <typeAlias alias="EHCACHE"
* type="com.ibatis.sqlmap.engine.cache.ehcache.EhCacheController" />
* </sqlMapConfig>
* </code>
* </pre>
*
*/
public class EhCacheController implements CacheController { /**
* The EhCache CacheManager.
*/
private CacheManager cacheManager; public static final String CONFIG_LOCATION = "configLocation"; /**
* Default Configure Location
*/
public static final String DEFAULT_CONFIG_LOCATION = "/ehcache.xml"; /**
* Flush a cache model.
*
* @param cacheModel
* - the model to flush.
*/
public void flush(CacheModel cacheModel) {
getCache(cacheModel).removeAll();
} /**
* Get an object from a cache model.
*
* @param cacheModel
* - the model.
* @param key
* - the key to the object.
* @return the object if in the cache, or null(?).
*/
public Object getObject(CacheModel cacheModel, Object key) {
Object result = null;
Element element = getCache(cacheModel).get(key);
if (element != null) {
result = element.getObjectValue();
}
return result; } /**
* Put an object into a cache model.
*
* @param cacheModel
* - the model to add the object to.
* @param key
* - the key to the object.
* @param object
* - the object to add.
*/
public void putObject(CacheModel cacheModel, Object key, Object object) {
getCache(cacheModel).put(new Element(key, object));
} /**
* Remove an object from a cache model.
*
* @param cacheModel
* - the model to remove the object from.
* @param key
* - the key to the object.
* @return the removed object(?).
*/
public Object removeObject(CacheModel cacheModel, Object key) {
Object result = this.getObject(cacheModel, key);
getCache(cacheModel).remove(key);
return result;
} /**
* Gets an EH Cache based on an iBatis cache Model.
*
* @param cacheModel
* - the cache model.
* @return the EH Cache.
*/
private Cache getCache(CacheModel cacheModel) {
String cacheName = cacheModel.getId();
Cache cache = cacheManager.getCache(cacheName);
return cache;
} /**
* Shut down the EH Cache CacheManager.
*/
public void finalize() {
if (cacheManager != null) {
cacheManager.shutdown();
}
} /**
* Configure a cache controller. Initialize the EH Cache Manager as a
* singleton.
*
* @param props
* - the properties object continaing configuration information.
*/
@Override
public void configure(Properties props) {
String configLocation = props.getProperty(CONFIG_LOCATION);
// if can not found ehcache.xml from configLocaion,
// use default configure file.
if (configLocation == null) {
configLocation = DEFAULT_CONFIG_LOCATION;
}
URL url = getClass().getResource(configLocation);
cacheManager = CacheManager.create(url);
}
}
这里默认在根目录下获取ehcache.xml文件,可以通过cacheModel配置进行修改。
在SqlMapConfig.xml文件中配置一个别名,作为全局变量,供其余SqlMap使用
<typeAlias
alias="EHCACHE"
type="com.ibatis.sqlmap.engine.cache.ehcache.EhCacheController" />
顺便提一句,要使用缓存注意SqlMapConfig.xml文件中settings节点配置cacheModelsEnabled为true!
<settings
cacheModelsEnabled="true"
useStatementNamespaces="true"
...
/>
如果要变更ehcache.xml文件路径为/config/ehcache.xml,可以在上述节点中下入如下代码:
<property name="configLocation" value="/config/ehcache.xml" />
然后,就可以通过ehcache.xml控制ehcache缓存了!![]()
举例说明iBatis SqlMap & ehcahce.xml,以免有些兄弟混淆!
以Account类为示例,在SqlMap中的配置为:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap
namespace="Account">
<cacheModel
id="cache"
type="EHCACHE">
<flushInterval
hours="1" />
<!-- flush操作,需要指明 Namespace -->
<flushOnExecute
statement="Account.create" />
</cacheModel>
<typeAlias
alias="account"
type="org.zlex.acl.Account" />
<resultMap
id="accountMap"
class="account">
<result
property="accountId"
column="accountId" />
<result
property="accountName"
column="accountName" />
<result
property="mail"
column="mail" />
<result
property="realName"
column="realName" />
<result
property="status"
column="status" />
<result
property="lastLoginTime"
column="lastLoginTime" />
</resultMap>
<select
id="readByAccountName"
parameterClass="string"
resultMap="accountMap"
cacheModel="cache">
<![CDATA[
SELECT
accountId,
accountName,
mail,
realName,
status,
lastLoginTime
FROM
acl_account
WHERE
accountName = #accountName#
]]>
</select>
<insert
id="create"
parameterClass="account">
<![CDATA[
INSERT INTO
acl_account(
accountName,
mail,
realName,
status,
lastLoginTime
)
VALUES (
#accountName#,
#mail#,
#realName#,
#status#,
#lastLoginTime#
)
]]>
<selectKey
resultClass="long"
keyProperty="accountId">
<![CDATA[select LAST_INSERT_ID() as id ]]>
</selectKey>
</insert>
</sqlMap>
注意:
<select
id="readByAccountName"
parameterClass="string"
resultMap="accountMap"
cacheModel="cache">
这里的cacheModel="cache",对应的在ehcache.xml中就应该是:
<cache
name="Account.cache"
maxElementsInMemory="10000"
eternal="false"
maxElementsOnDisk="1000"
overflowToDisk="true"
timeToIdleSeconds="300"
/>
因为,我使用了useStatementNamespaces="true"![]()
缓存初解(四)---Ibatis的缓存配置+Ehcache的更多相关文章
- 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache
本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...
- Spring自定义缓存管理及配置Ehcache缓存
spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...
- 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...
- 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存
jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...
- Spring Boot中的缓存支持(一)注解配置与EhCache使用
Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...
- MyBatis学习总结(四)——MyBatis缓存与代码生成
一.MyBatis缓存 缓存可以提高系统性能,可以加快访问速度,减轻服务器压力,带来更好的用户体验.缓存用空间换时间,好的缓存是缓存命中率高的且数据量小的.缓存是一种非常重要的技术. 1.0.再次封装 ...
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...
- 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...
随机推荐
- 【Sql Server】使用触发器把一个表中满足条件的数据部分字段插入到另一个表中
create trigger 触发器名称 on 对哪个表起作用 after insert,update as return set nocount on begin transaction; inse ...
- 【Qt】Qt Creator介绍【转】
简介 Qt Creator是使用Qt开发的IDE.Qt支持Windows.Linux/Unix.Mac OS X.Android.BlackBerry.QNX等多种平台,Qt Creator为不同平台 ...
- 本地安装gem install --local redis-stat-0.4.13.gem
因为主机环境不能联外网,悲哀,所以只能想办法下载包,上传到主机来安装 环境:el6.x86_64 1. gem 安装[http://centos.ustc.edu.cn/centos/6/os/x86 ...
- sql语句中like匹配的用法详解
在SQL结构化查询语言中,LIKE语句有着至关重要的作用. LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用 ...
- 左右滑动删除ListView条目Item--第三方开源--SwipeToDismiss
Android的SwipeToDismiss是github上一个第三方开源框架(github上的项目链接地址:https://github.com/romannurik/Android-SwipeTo ...
- 第25章 项目6:使用CGI进行远程编辑
初次实现 25-1 simple_edit.cgi --简单的网页编辑器 #!D:\Program Files\python27\python.exeimport cgiform = cgi.Fiel ...
- 语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient)
语音识别之梅尔频谱倒数MFCC(Mel Frequency Cepstrum Coefficient) 原理 梅尔频率倒谱系数:一定程度上模拟了人耳对语音的处理特点 预加重:在语音信号中,高频部分的能 ...
- [原]项目进阶 之 持续构建环境搭建(二)Nexus私服器
上一篇博文项目进阶 之 持续构建环境搭建(一)架构中,我们大致讲解了一下本系列所搭建环境的基本框架,这次开始我们进入真正的环境搭建实战.重点不在于搭建的环境是否成功和完善,而是在搭建过程中充分认识到每 ...
- UML 小结(3)- UML的结构及各个阶段的应用
UML的结构: 其中各个图的作用如下: 用例图:用来描述用户的需求,从用户的角度描述系统的功能,并指出各功能的执行者,强调谁在使用系统,系统为执行者完成哪些功能. 静态图包括类图跟对象图 类图 ...
- java synchronized(一)
java synchronized主要用于控制线程同步,中间有很多小的细节,知识,这里我简单的整理一下,做个记录.主要用于方法和代码块的控制 先说说方法控制 模拟银行存款和取款,创建一个Account ...