ehcahce 的页面缓存 配置简单

来源:互联网 发布:mac系统绘画软件 编辑:程序博客网 时间:2024/05/18 17:00

地址:http://ljh0721.iteye.com/blog/1797987

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的过滤器之前

Java代码 复制代码 收藏代码
  1. <filter>   
  2.   <filter-name>CachePageCachingFilter</filter-name>   
  3.   <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter   
  4.   </filter-class>   
  5.   <init-param>   
  6.     <param-name>suppressStackTraces</param-name>   
  7.     <param-value>false</param-value>   
  8.   </init-param>   
  9.   <init-param>   
  10.     <param-name>cacheName</param-name>   
  11.     <param-value>CachePageCachingFilter</param-value>   
  12.   </init-param>   
  13. </filter>   
  14. <filter-mapping>    
  15.   <filter-name>CachePageCachingFilter</filter-name>    
  16.   <url-pattern>*.action</url-pattern>    
  17. </filter-mapping>    

2、在ehcache.xml中配置

Java代码 复制代码 收藏代码
  1. <cache name="CachePageCachingFilter"   
  2.          maxElementsInMemory="10"<!- 缓存最大数目 -> 
  3.          eternal="false" <!- 缓存是否持久 -> 
  4.          overflowToDisk="true" <!- 当系统当机时,是否保存到磁盘 -> 
  5.          timeToIdleSeconds="120"  <!- 当缓存闲置 n 秒后销毁 ->  
  6.          timeToLiveSeconds="120"  <!- 当缓存存活 n 秒后销毁-> 
  7.          memoryStoreEvictionPolicy = "LFU"> <!- 缓存清除策略 ->  
  8.   </cache> 
简单配置到这里就结束了

了解 ehcache的几个概念,

   timeToIdleSeconds ,多长时间不访问该缓存,那么 ehcache 就会清除该缓存。

   timeToLiveSeconds ,缓存的存活时间,从开始创建的时间算起。

Ehcache的三种清空策略:

1.   FIFO,first in first out ,这个是大家最熟的,先进先出。

2.   LFU, Less Frequently Used ,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个 hit 属性,hit 值最小的将会被清出缓存。

3.   LRU,Least Recently Used ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

原创粉丝点击