Ehcache緩存之web應用實例
Ehcache緩存之web應用實例
前面學習了緩存的基本配置和使用,下面繼續學習如何緩存一個頁面
我搭建的環境是 spring+freemarker+Maven ,沒有使用hibernate配置緩存 ,想先弄個簡單的頁面緩存看看
需要jar包:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-web</artifactId>
<version>2.0.4</version>
</dependency>
可以加上這個倉庫 https://oss.sonatype.org/content/groups/sourceforge/
我測試ehcache配置如下
<ehcache updateCheck="false" dynamicConfig="false">!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <diskStore path="d:\\ehcache\\tmpdir"/> <cacheManagerEventListenerFactory class="" properties=""/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="1200000" timeToLiveSeconds="1200000" overflowToDisk="true" /> <cache name="icache-global" maxElementsInMemory="1" maxElementsOnDisk="1" eternal="true" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="true" /> <!-- 測試用 --> <cache name="<span style="color:#e53333;font-weight:bold;">SimplePageCachingFilter</span>" maxElementsInMemory="10" maxElementsOnDisk="10" eternal="false" overflowToDisk="true" timeToIdleSeconds="100" timeToLiveSeconds="30" memoryStoreEvictionPolicy="LFU" /> <cache name="SimplePageFragmentCachingFilter" maxElementsInMemory="1" maxElementsOnDisk="1" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" />
</ehcache></pre>
首先學習下緩存配置中幾個重要的參數配置
<cache name="SimplePageCachingFilter"
maxElementsInMemory="10" //這個是表示SimplePageCachingFilter緩存在內存中的element數量
maxElementsOnDisk="10" //這個是表示SimplePageCachingFilter緩存在磁盤中的element數量
eternal="false" //說明該緩存會死亡
overflowToDisk="true" //設置是否溢出是是否存到磁盤上
timeToIdleSeconds="10" //多長時間不訪問該緩存,那么ehcache 就會清除該緩存.
timeToLiveSeconds="30" //緩存的存活時間,從開始創建的時間算起.
memoryStoreEvictionPolicy="LFU" //緩存的清空策略
/>
ehcache 中緩存的3 種清空策略:
1 FIFO ,first in first out ,這個是大家最熟的,先進先出,不多講了
2 LFU , Less Frequently Used ,就是上面例子中使用的策略,直白一點就是講一直以來最少被使用的.如上面所講,緩存的元素有一個hit 屬性,hit 值最小的將會被清出緩存.
2 LRU ,Least Recently Used ,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存.
緩存的其他知識可以去百度去,這里介紹緩存的存儲方式和一些原理
下面是web.xml方面的配置
<!-- ehcache --> <filter> <filter-name>SimplePageCachingFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter </filter-class> </filter><!-- This is a filter chain. They are executed in the order below. Do not change the order. --> <filter-mapping> <filter-name>SimplePageCachingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping></pre>
注意: ehcache.xml文件中配置的緩存名稱 必須和web.xml配置的緩存filter名字一致 不然會拋出找不到配置的異常 ;并且必須把ehcache.xml放在class根目錄下。
接下來就是寫一個Controller了
@Controller public class EhcacheController extends BaseAction {@RequestMapping(value = "ehcache/save.do" ,method=RequestMethod.GET) public String index(Locale locale, Model model,HttpServletRequest request, HttpServletResponse response) throws Exception{ logger.info("welcome to daotie home ,the client locale is "+ locale.toString()); return "ehcache/show"; }
}</pre>
我寫的是 spring的mvc實現,接下來我們就需要看ehcache/show.ftl頁面看看是否用了緩存
你可以再訪問一次后,里面修改show.ftl頁面內容,會發現沒有變化,等過了30秒(因為我在緩存中配置的SimplePageCachingFilter的緩存存活時間是30秒)后就變了,說明緩存起了作用了。