ehcache技术详解Word下载.docx
《ehcache技术详解Word下载.docx》由会员分享,可在线阅读,更多相关《ehcache技术详解Word下载.docx(12页珍藏版)》请在冰点文库上搜索。
UTF-8"
>
ehcachexmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
xsi:
noNamespaceSchemaLocation="
//ehcache.org/ehcache.xsd"
<
diskStorepath="
java.io.tmpdir"
/>
cachename="
sampleCache1"
maxEntriesLocalHeap="
10000"
maxEntriesLocalDisk="
1000"
eternal="
false"
overflowToDisk="
true"
diskPersistent="
diskSpoolBufferSizeMB="
20"
timeToIdleSeconds="
300"
timeToLiveSeconds="
600"
memoryStoreEvictionPolicy="
LFU"
diskExpiryThreadIntervalSeconds="
120"
transactionalMode="
off"
cacheEventListenerFactory
class="
com.ehcache.EhcacheListenerTest"
properties="
wxwtestkey=200,wxtestkey2=300"
persistencestrategy="
localTempSwap"
/cache>
/ehcache>
配置项详解:
此配置项配置的是,磁盘缓存的位置。
其中java.io.tmpdir的具体位置可以由java代码,System.getProperty("
)来查看。
此处的path也可以配置成user.home,user.dir或用户设定的系统变量指定的路径。
Cache元素表示一个一个缓存区域,在ehcache.xml中可以有多个cache,他们之间由name属性来区分。
maxEntriesLocalHeap="
:
表示内存中最大可以存在10000个元素。
maxEntriesLocalDisk="
表示磁盘中最大可以存在1000个元素
eternal="
表示缓存是否持久,false表示不持久可被销毁,true表示一直存在。
overflowToDisk="
是否保存到磁盘,当内存中的元素溢出时。
diskPersistent="
是否保存到磁盘,当进程重启时。
diskSpoolBufferSizeMB="
保存到磁盘的后台线程的缓存为20M,当数据从内存中转到磁盘时,会先将数据转入此缓存,然后由后台线程将数据从缓存中持久化到磁盘。
timeToIdleSeconds="
表示缓存空闲300s后被销毁。
timeToLiveSeconds="
表示缓存最长可以存在300s,然后被销毁。
memoryStoreEvictionPolicy="
Ehcache的三种清空策略,FIFO:
先进先出。
LFU:
一直以来最少被使用的,即缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
LRU:
最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
diskExpiryThreadIntervalSeconds="
检查磁盘上的缓存超期的线程的运行周期为120s。
transactionalMode="
事务关闭(ehcache中的事务是怎么使用的?
)
3使用
3.1Ehcache的类层次结构
3.2Ehcache基本用法
使用如下:
1、获取manager,如果ehcache.xml不是在src下,可以由create(“config/ehcache.xml”)来指定路径。
CacheManagermanager=CacheManager.create();
2、获取缓存区,根据ehcache.xml中配置的cache节点,指定name来获取cache.
Cachecache=manager.getCache("
);
3、创建key-value形式的元素,存入缓存区,或从缓存区取出。
Elementelement=newElement("
key1"
"
wxwtest"
cache.put(element);
Elementfind=cache.get("
4、关闭mananger.
manager.shutdown();
另外,Ehcache还提供了监听的机制,在如上的配置文件中:
其中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)
==notifyElementEvicted"
publicvoidnotifyElementExpired(Ehcacheehcache,Elementelement)
==notifyElementExpired"
publicvoidnotifyElementPut(Ehcacheehcache,Elementelement)
throwsCacheException
==notifyElementPut"
publicvoidnotifyElementRemoved(Ehcacheehcache,Elementelement)
==notifyElementRemoved"
publicvoidnotifyElementUpdated(Ehcacheehcache,Elementelement)
==notifyElementUpdated"
publicvoidnotifyRemoveAll(Ehcacheehcache)
==notifyRemoveAll"
}
通过监听,在对cache做操作时,调用相应的监听类方法。
ehcache的基本用法就是这样。
根据以上的基本用法,我们就更容易理解以下ehcache的一些复杂应用。
3.3Ehcache+Mybatis做DAO缓存
1、在搭建好ehcache和Mybatis的基础上,引入mybatis-ehcache-xxx.jar。
2、在ehcache.xml中将默认的缓存区配置上,如下:
defaultCache
60"
30"
10000000"
diskExpiryThreadIntervalSeconds="
LRU"
/defaultCache>
3、修改Mapper.xml,加上<
cache节点,例如:
?
!
DOCTYPEmapperPUBLIC"
-//mybatis.org//DTDMapper3.0//EN"
"
//mybatis.org/dtd/mybatis-3-mapper.dtd"
>
mappernamespace="
com.mybatis.dao.TestDAO"
cachetype="
org.mybatis.caches.ehcache.EhcacheCache"
—也可以是<
org.mybatis.caches.ehcache.LoggingEhcache"
-->
resultMapid="
BaseResultMap"
……
/resultMap>
selectid="
getTestBean"
resultMap="
xxx"
parameterType="
java.util.Map"
……
/select>
/mapper>
4、mybatis默认是启用cache的,所以对于某一条不想被cache的sql需要把useCache="
加上。
例如:
getCategory"
string"
resultType="
useCache="
……
注:
貌似单独的Mybatis用不了缓存。
测试了没有成功。
3.4Ehcache+Spring做业务层缓存
1、引入相应jar包,spring3.2.xxx所需要的jar,cglib-xxx.jar,ehcache-spring-annotations-xxxx.jar,google-collections-xx.jar
2、配置spring
beansxmlns="
//www.springframework.org/schema/beans"
xmlns:
ehcache="
//ehcache-spring-
schemaLocation="
http:
//www.springframework.org/schema/beans
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
ehcache:
annotation-drivencache-manager="
ehCacheManager"
/>
beanid="
class="
org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
<
propertyname="
configLocation"
value="
classpath:
ehcache.xml"
/property>
/bean>
/beans>
3、配置ehcache.xml
cache
name="
metaColumnCache"
maxElementsInMemory="
100"
0"
4、使用,在业务方法上加相应注释
@Cacheable(cacheName="
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中配置一个用于缓存网站首页的缓存区,如下:
indexCache"
2"
3、在web.xml中为添加首页的filter
filter>
filter-name>
indexCacheFilter<
/filter-name>
<
filter-class>
net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter<
/filter-class>
init-param>
<
param-name>
cacheName<
/param-name>
param-value>
indexCache<
/param-value>
/init-param>
/filter>
filter-mapping>
url-pattern>
/index.jsp<
/url-pattern>
/filter-mapping>
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中的
配置。