Ehcache Controller class
package com.ibatis.sqlmap.engine.cache.EhCacheController;
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:
* <cacheModel id="myCache" type="nl.rabobank.springproject.ibatis.EhCacheController" readOnly="true" serialize="false">
* <property name="configFile" value="/path-to-ehcache.xml"/>
* </cacheModel>
* Alternatively, you can use a type alias in your type attribute and defining the class with a
* <TypeAlias> declaration, see iBatis documentation on how to do this.
*/
public class EhCacheController implements CacheController {
/** The EhCache CacheManager. */
private CacheManager cacheManager;
/**
* 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;
}
/**
* Configure a cache controller. Initialize the EH Cache Manager as a singleton.
* @param props - the properties object continaing configuration information.
*/
public void setProperties(Properties props) {
URL url = getClass().getResource(props.getProperty("configFile"));
cacheManager = CacheManager.create(url);
}
/**
* 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();
}
}
}
A Sample 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="myNamespace">
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="MyPojo" type="nl.myproject.MyPojo"/>
<typeAlias alias="MapCacheController" type="com.ibatis.sqlmap.engine.cache.EhCacheController"/>
<cacheModel id="MyPojoCache" type="MapCacheController" readOnly="true" serialize="false">
<property name="configFile" value="/ehcache.xml"/>
</cacheModel>
<resultMap id="ResultQueryMap" class="MyPojo">
...property mappings go here...
</resultMap>
<select id="getMyPojoList" resultMap="ResultQueryMap" cacheModel="MyPojoCache">
...select query to get your pojo from the database goes here...
</select>
</sqlMap>
A sample ehcache 配置文件
<ehcache>
...put your default cache here...
<cache
name="myNamespace.MyPojoCache"
maxElementsInMemory="5"
eternal="false"
timeToLiveSeconds="60"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
总结:
1.你可以建多个CacheModel 在sqlmap的配置文件中,然后指定到同一个cacheController.
2.记住不要忘了 设置 cacheModelsEnabled=true (在sqlmap的<setting>)否则你的cache就根本不会去作用。
分享到:
相关推荐
spring+ibatis+ehcache整合完整例子,数据库MySQL
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
hibernate+ehcache hibernate+ehcache hibernate+ehcache hibernate+ehcache hibernate+ehcache
文件中包括ehcache-core-sources.jar和mybatis-ehcache-sources.jar这两个jar的源码
自己项目的开发包集合,其中包括:缓存处理ehcache相关jar,spring MVC4.0 jar,ehcache依赖jar,以及其他jar(图像处理thumbnailator-0.4.2),包虽然不是很新但可用。实际使用时找包较为麻烦,现在整理出来,希望...
使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。
网上有许多cas和shiro的集成,但是有用的没有什么完整代码,我这里实现了整个过程,并且测试通过,希望能够给大家帮助!有图有真相!
使用Intellj IDEA 集成开发工具 搭建的maven项目,使用Spring+Hibernate+Shiro+Ehcache集成,完成一个简单的用户角色菜单项目
spring3整合EhCache注解实例
spring boot 集成activemq Datajpa Ehcache 里面有详细的例子
ssh,struts+hibernate+spring+ehcache集成
shiro-demo使用ehcache做缓存.zip
NULL 博文链接:https://cczakai.iteye.com/blog/1622708
spring集成ehcache所需的jar包
1.解压缩到目录下,复制ehcache-monitor-kit-1.0.0\lib\ehcache-probe-1.0.0.jar包到application的web-inf/lib目录下 2.将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名...
该压缩包用于缓存服务部署,jboss集成ehcache,解压启动即可访问,实现缓存共用
包括ehcache-core-2.4.3.jar、ehcache-web-2.0.4.jar、guava-15.0.jar、slf4j-api-1.7.12.jar、commons-logging.jar springMVC集成Ehcache你会用得到。
SpringBoo2.x整合Ehcache3.x作为缓存底层存储。
ehcache-3.8.1.jar
EhCache