配置:
一.在src目录下加入ehcache.xml:
< cache name = "SimplePageCachingFilter"
maxElementsInMemory = "10"
maxElementsOnDisk = "10"
eternal = "false"
overflowToDisk = "true"
diskSpoolBufferSizeMB = "20"
timeToIdleSeconds = "10"
timeToLiveSeconds = "10"
memoryStoreEvictionPolicy = "LFU" />
在使用ehcache 的页面缓存之前,我们必须要了解ehcache 的几个概念,
1 timeToIdleSeconds ,多长时间不访问该缓存,那么ehcache 就会清除该缓存。
2 timeToLiveSeconds ,缓存的存活时间,从开始创建的时间算起。
SimplePageCachingFilter 是缓存的名字,maxElementsInMemory 表示内存中SimplePageCachingFilter 缓存中元素的最大数量为10 ,maxElementsOnDisk 是指持久化该缓存的元素到硬盘上的最大数量也为10 (),eternal=false 意味着该缓存会死亡。overflowToDisk=true 意思是表示当缓存中元素的数量超过限制时,就把这些元素持久化到硬盘,如果overflowToDisk 是false ,那么maxElementsOnDisk 的设置就没有什么意义了。memoryStoreEvictionPolicy=LFU 是指按照缓存的hit 值来清除,也就是说缓存满了之后,新的对象需要缓存时,将会将缓存中hit 值最小的对象清除出缓存,给新的对象腾出地方来了。
ehcache 中缓存的3 种清空策略:
1 FIFO ,first in first out ,这个是大家最熟的,先进先出,不多讲了
2 LFU , Less Frequently Used ,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
3 LRU ,Least Recently Used ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
<filter></filter>《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》/search/companySearch.action?actionName=companySearch《/url-pattern》
《/filter-mapping》
三。hbm.xml
《 cache usage="read-only"? /》
四。impl.java
Query q1 = this.getSession().createQuery(hql.toString()).setFirstResult(0).setMaxResults(10).setCacheable(true)
. setCacheRegion(" SimplePageCachingFilter ");
setCacheable(true):查询的结果需要缓存起来。
setCacheRegion():指用ehcache.xml里的命名为 SimplePageCachingFilter的 缓存条目。
其他的页面不需要做任何的设置就可以使用了。。而且在缓存期间就不会出现大块的sql执行了。。。哈哈。。任务完成。
本文介绍了如何使用EHCache进行页面缓存配置。包括在src目录下配置ehcache.xml文件,设置缓存策略如LFU,并在web.xml中注册SimplePageCachingFilter过滤器。此外还涉及如何在Hibernate查询中启用缓存。
382

被折叠的 条评论
为什么被折叠?



