ehcache做页面部分缓存,配置有点麻烦,操作也有点复杂,不过感觉还是很好用。
首先看web.xml配置:
<!--ehcache web page cache -->
<filter>
<filter-name>fragmentCache</filter-name>
<filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter
</filter-class>
<init-param>
<param-name>suppressStackTraces</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cacheName</param-name>
<param-value>fragmentCache</param-value>
</init-param>
</filter>
<!--
This is a filter chain. They are executed in the order below. Do not
change the order.
-->
<filter-mapping>
<filter-name>fragmentCache</filter-name>
<url-pattern>/WEB-INF/pages/tour/tourDetailBody.jsp</url-pattern>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="10"
overflowToDisk="true" />
<!--
maxElementsInMemory="10"内存中的最大页面对象
eternal="false"
timeToIdleSeconds="120" timeToIdleSeconds ,多长时间不访问该缓存,那么ehcache就会清除该缓存。
timeToLiveSeconds="240" timeToLiveSeconds,缓存的存活时间,从开始创建的时间算起。
overflowToDisk="true" 是否写入硬盘
-->
<!-- Page and Page Fragment Caches -->
<cache name="fragmentCache"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="10000"
timeToLiveSeconds="10000"
overflowToDisk="true">
</cache>
</ehcache>
1,下载EhCache web版 http://ehcache.org/downloads/catalog 注意下载web版。
2,解压 将ehcache-web-2.0.3.jar 、ehcache-core-2.3.0.jar 拷入lib中。