ehcahce 的页面缓存 配置简单
需要导入ehcache-web-2.0.4.jar 和ehcache-core-2.6.3.jar (页面缓存web包是必须要有的)可从http://sourceforge.net/projects/ehcache/files/下载最新的jar包
1、在web.xml中配置(在struts2的过滤器之前)
<filter>
<filter-name>CachePageCachingFilter</filter-name>
<filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
</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>CachePageCachingFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CachePageCachingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
2、在ehcache.xml中配置
<cache name="CachePageCachingFilter"
maxElementsInMemory="10"<!- 缓存最大数目 ->
eternal="false" <!- 缓存是否持久 ->
overflowToDisk="true" <!- 当系统当机时,是否保存到磁盘 ->
timeToIdleSeconds="120" <!- 当缓存闲置 n 秒后销毁 ->
timeToLiveSeconds="120" <!- 当缓存存活 n 秒后销毁->
memoryStoreEvictionPolicy = "LFU"> <!- 缓存清除策略 ->
</cache>
简单配置到这里就结束了
了解 ehcache 的几个概念,
timeToIdleSeconds ,多长时间不访问该缓存,那么 ehcache 就会清除该缓存。
timeToLiveSeconds ,缓存的存活时间,从开始创建的时间算起。
Ehcache 的三种清空策略:
1. FIFO ,first in first out ,这个是大家最熟的,先进先出。
2. LFU , Less Frequently Used ,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个 hit 属性,hit 值最小的将会被清出缓存。
3. LRU ,Least Recently Used ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。