ehcache页面缓存

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 ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值