class="com.ehcache.EhcacheListenerTest"
properties="wxwtestkey=200,wxtestkey2=300"/>
其中class是自定义的实现类,继承CacheEventListenerFactory,覆盖createCacheEventListener(Propertiesprops)方法。
EhcacheListenerTest代码如下所示:
packagecom.ehcache;
importjava.util.Properties;
importnet.sf.ehcache.CacheException;
importnet.sf.ehcache.Ehcache;
importnet.sf.ehcache.Element;
importnet.sf.ehcache.event.CacheEventListener;
importnet.sf.ehcache.event.CacheEventListenerFactory;
publicclassEhcacheListenerTestextendsCacheEventListenerFactory
{
@Override
publicCacheEventListenercreateCacheEventListener(Propertiesprops)
{
System.out.println("==createlistener==");
for(Objectkey:
props.keySet())
{
System.out.println(key+"="+props.get(key));
}
returnnewTestCacheEventListener();
}
classTestCacheEventListenerimplementsCacheEventListener
{
publicObjectclone()
{
returnnewTestCacheEventListener();
}
publicvoiddispose()
{
System.out.println("==dispose");
}
publicvoidnotifyElementEvicted(Ehcacheehcache,Elementelement)
{
System.out.println("==notifyElementEvicted");
}
publicvoidnotifyElementExpired(Ehcacheehcache,Elementelement)
{
System.out.println("==notifyElementExpired");
}
publicvoidnotifyElementPut(Ehcacheehcache,Elementelement)
throwsCacheException
{
System.out.println("==notifyElementPut");
}
publicvoidnotifyElementRemoved(Ehcacheehcache,Elementelement)
throwsCacheException
{
System.out.println("==notifyElementRemoved");
}
publicvoidnotifyElementUpdated(Ehcacheehcache,Elementelement)
throwsCacheException
{
System.out.println("==notifyElementUpdated");
}
publicvoidnotifyRemoveAll(Ehcacheehcache)
{
System.out.println("==notifyRemoveAll");
}
}
}
通过监听,在对cache做操作时,调用相应的监听类方法。
ehcache的基本用法就是这样。
根据以上的基本用法,我们就更容易理解以下ehcache的一些复杂应用。
3.3Ehcache+Mybatis做DAO缓存
1、在搭建好ehcache和Mybatis的基础上,引入mybatis-ehcache-xxx.jar。
2、在ehcache.xml中将默认的缓存区配置上,如下:
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="300"
diskSpoolBufferSizeMB="30"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
3、修改Mapper.xml,加上xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http:
//mybatis.org/dtd/mybatis-3-mapper.dtd">
—也可以是-->
……
……
4、mybatis默认是启用cache的,所以对于某一条不想被cache的sql需要把useCache="false"加上。
例如:
……
注:
貌似单独的Mybatis用不了缓存。
测试了没有成功。
3.4Ehcache+Spring做业务层缓存
1、引入相应jar包,spring3.2.xxx所需要的jar,cglib-xxx.jar,ehcache-spring-annotations-xxxx.jar,google-collections-xx.jar
2、配置spring
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
ehcache="http:
//ehcache-spring-
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//ehcache-spring-
http:
//ehcache-spring-
annotation-drivencache-manager="ehCacheManager"/>
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
ehcache.xml">
3、配置ehcache.xml
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
4、使用,在业务方法上加相应注释
@Cacheable(cacheName="metaColumnCache")
publicListgetColumnModel(StringtableId){
……
}
3.5Ehcache做页面缓存
假如有这样一个需求:
给一个网站的首页做一个页面缓存,2分钟更新一次。
以应用结构page-filter-action-service-dao-db为例,页面缓存做到尽量靠近客户的地方,就是在page和filter之间,这样的优点就是第一个用户请求之后,页面被缓存,第二个用户再来请求的时候,走到filter这个请求就结束了,无需再走后面的action-service-dao-db。
带来的好处是服务器压力的减低和客户段页面响应速度的加快。
1、在搭建好ehcache的基础上引入ehcache-web-xxx.jar。
2、在ehcache.xml中配置一个用于缓存网站首页的缓存区,如下:
maxEntriesLocalHeap="100"
maxEntriesLocalDisk="100"
eternal="false"
diskSpoolBufferSizeMB="2"
timeToIdleSeconds="60"
timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
3、在web.xml中为添加首页的filter
indexCacheFilternet.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
cacheName
indexCache
indexCacheFilter
/index.jsp
Ok,页面缓存就是这么简单。
通过分析SimplePageCachingFilter的源码,我们可以看到,过滤器通过获取HttpServletRequest的httpRequest.getMethod()+httpRequest.getRequestURI()+httpRequest.getQueryString();
作为key,从入缓存中查询页面,若没有则读取页面放入缓存中,若有则直接从缓存中返回。
4卸载
1、去掉jar包:
ehcache-xxx.jar或ehcache-core-xxx.jar,ehcache-web-xxx.jar,mybatis-ehcache-xxx.jar。
2、去掉ehcache.xml配置文件。
3、页面缓存,去掉web.xml中的filter配置。
4、Mybatis缓存,去掉xxxMapper.xml中的
配置。