您的位置:首页 > 编程语言 > Java开发

EHCache页面缓存技术

2015-09-06 13:58 225 查看
摘要: EHcache可以用于页面缓存和对象缓存,是非常好的缓存产品,简单易用。这里介绍EHCache的页面缓存技术。

EHCache的jar包 :http://ehcache.org/downloads/catalog 这里用的是EHcache的web包。

在项目的web.xml文件中加入一个filter:

<!--EHCache 页面缓存技术,该页面缓存技术用于page -> filter之间,若是局部页面缓存,
则用SimplePageFragmentCachingFilter,但应注意局部页面要被其他页面 jsp:include
-->
<filter>
<filter-name>indexCacheFilter</filter-name>
<filter-class> net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
</filter>
<!-- 这里是需要缓存的页面 -->
<filter-mapping>
<filter-name>indexCacheFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>


在src中新建一个ehcache.xml文件:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd">
<diskStore path="java.io.tmpdir/ehcache" />

<!--
timeToIdleSeconds ,多长时间不访问该缓存,那么ehcache 就会清除该缓存
timeToLiveSeconds ,缓存的存活时间,从开始创建的时间算起
memoryStoreEvictionPolicy,采用哪种缓存算法
overflowToDisk=true 意思是表示当缓存中元素的数量超过限制时,就把这些元素持久化到硬盘
maxElementsInMemory 表示内存中SimplePageCachingFilter 缓存中元素的最大数量为10
maxElementsOnDisk 是指持久化该缓存的元素到硬盘上的最大数量也为10
eternal,缓存是否会死亡
-->
<cache name = "SimplePageCachingFilter"
maxElementsInMemory = "10"
maxElementsOnDisk = "10"
eternal = "false"
overflowToDisk = "true"
diskSpoolBufferSizeMB = "20"
timeToIdleSeconds = "120"
timeToLiveSeconds = "120"
memoryStoreEvictionPolicy = "LFU"/>
</ehcache>


ok,这样就对index.jsp做了缓存。另外,对于频繁更新的页面不应该使用缓存技术。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java EHCache 页面缓存