Echache整合Spring緩存實例講解

      林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka

             摘要:本文主要介紹了EhCache,並通過整合spring給出了一個使用實例。

一、EhCache 介紹

          EhCache 是一個純Java的進程內緩存框架,具有快速、精幹等特點,是hibernate中默認的CacheProvider。Ehcache是一種廣泛使用的開源Java分佈式緩存。主要面向通用緩存,Java EE和輕量級容器。它具有內存和磁盤存儲,緩存加載器,緩存擴展,緩存異常處理程序,一個gzip緩存servlet過濾器,支持REST和SOAP api等特點。

     它的一個缺點就是使用磁盤Cache的時候非常佔用磁盤空間,這源於DiskCache的算法簡單,該算法簡單也導致Cache的效率非常高。它只是對元素直接追加存儲。因此搜索元素的時候非常的快。如果使用DiskCache的,在很頻繁的應用中,很快磁盤會滿。
另外一個問題是當突然kill掉java的時候,不能保證數據的安全,可能是產生衝突,Ehcache的解決方法是如果文件衝突了,則重建cache。這對於Cache數據需要保存的時候可能不利。當然,Cache只是簡單的加速,而不能保證數據的安全。如果想保證數據的存儲安全,可以使用Bekeley DB Java Edition版本。這是個嵌入式數據庫。可以確保存儲安全和空間的利用率。當然,還有很多的Cache。多數情況下,Ehcache能滿足常見需求。


二、使用實例

本文要使用

1、引入jar包

[html] view plain copy
  1.     <dependency>  
  2.     <groupId>net.sf.ehcache</groupId>  
  3.     <artifactId>ehcache</artifactId>  
  4.     <version>2.8.2</version>  
  5. </dependency>  

2、在classpath下增加ehcache配置文件 ehcache.xml與配置文件


設置控制ehcache的bean,這部分的內容也可以直接寫在spring的配置文件applicationContext.xml中去,這裏爲了方便管理,我給單獨寫出來了

beans-cache.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xsi:schemaLocation="   
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            
  7.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">  
  8.   
  9.     <cache:annotation-driven cache-manager="cacheManager" />  
  10.   
  11.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
  12.         <property name="cacheManager" ref="ehcache"></property>  
  13.     </bean>  
  14.   
  15.     <bean id="ehcache"  
  16.         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  17.         <property name="configLocation" value="classpath:beans/ehcache.xml"></property>  
  18.     </bean>  
  19.   
  20. </beans>   

ehcache.xml設置緩存大小和時間,緩存空間名等。其中緩存空間可設置多個

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache updateCheck="false">  
  3.   
  4.     <diskStore path="java.io.tmpdir" />  
  5.   
  6.     <!-- DefaultCache setting. -->  
  7.     <defaultCache eternal="false"   
  8.                   overflowToDisk="false"   
  9.                   diskPersistent="false"   
  10.                   timeToLiveSeconds="36000"   
  11.                   timeToIdleSeconds="36000"   
  12.                   maxElementsInMemory="10000"   
  13.                   memoryStoreEvictionPolicy="LRU"/>  
  14.   
  15.     <!-- Special objects setting. -->  
  16.     <!-- Refresh sysParamCache every hour. -->  
  17.     <cache name="sysParamCache"   
  18.            overflowToDisk="false"   
  19.            eternal="false"   
  20.            diskPersistent="false"   
  21.            timeToLiveSeconds="36000"   
  22.            timeToIdleSeconds="36000"   
  23.            maxElementsInMemory="10000"   
  24.            memoryStoreEvictionPolicy="LRU"/>  
  25.       
  26. </ehcache>  

參數說明:
<diskStore>:

當內存緩存中對象數量超過maxElementsInMemory時,將緩存對象寫到磁盤緩存中(需對象實現序列化接口)。
<diskStore path="">:

用來配置磁盤緩存使用的物理路徑,Ehcache磁盤緩存使用的文件後綴名是*.data和*.index。
name:

緩存名稱,cache的唯一標識(ehcache會把這個cache放到HashMap裏)。
maxElementsOnDisk:

磁盤緩存中最多可以存放的元素數量,0表示無窮大。
maxElementsInMemory:

內存緩存中最多可以存放的元素數量,若放入Cache中的元素超過這個數值,則有以下兩種情況。
1)若overflowToDisk=true,則會將Cache中多出的元素放入磁盤文件中。
2)若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素。
Eternal:

緩存中對象是否永久有效,即是否永駐內存,true時將忽略timeToIdleSeconds和timeToLiveSeconds。
timeToIdleSeconds:

緩存數據在失效前的允許閒置時間(單位:秒),僅當eternal=false時使用,默認值是0表示可閒置時間無窮大,此爲可選屬性即訪問這個cache中元素的最大間隔時間,若超過這個時間沒有訪問此Cache中的某個元素,那麼此元素將被從Cache中清除。
timeToLiveSeconds:

緩存數據在失效前的允許存活時間(單位:秒),僅當eternal=false時使用,默認值是0表示可存活時間無窮大,即Cache中的某元素從創建到清楚的生存時間,也就是說從創建開始計時,當超過這個時間時,此元素將從Cache中清除。
overflowToDisk:

內存不足時,是否啓用磁盤緩存(即內存中對象數量達到maxElementsInMemory時,Ehcache會將對象寫到磁盤中),會根據標籤中path值查找對應的屬性值,寫入磁盤的文件會放在path文件夾下,文件的名稱是cache的名稱,後綴名是data。

diskPersistent:

是否持久化磁盤緩存,當這個屬性的值爲true時,系統在初始化時會在磁盤中查找文件名爲cache名稱,後綴名爲index的文件,這個文件中存放了已經持久化在磁盤中的cache的index,找到後會把cache加載到內存,要想把cache真正持久化到磁盤,寫程序時注意執行net.sf.ehcache.Cache.put(Element element)後要調用flush()方法。
diskExpiryThreadIntervalSeconds:

磁盤緩存的清理線程運行間隔,默認是120秒。
diskSpoolBufferSizeMB:

設置DiskStore(磁盤緩存)的緩存區大小,默認是30MB
memoryStoreEvictionPolicy:

內存存儲與釋放策略,即達到maxElementsInMemory限制時,Ehcache會根據指定策略清理內存,共有三種策略,分別爲LRU(最近最少使用)、LFU(最常用的)、FIFO(先進先出)。

3.spring配置文件 applicationContext.xml 添加ehcache的配置

[plain] view plain copy
  1. <import resource="classpath:beans/beans-cache.xml" />   

4.在service層增加註解配置

[java] view plain copy
  1. @Override  
  2. @Cacheable(value="sysParamCache",key="#systemId+#merchantId+#businessType")// 使用了一個緩存名叫 accountCache     
  3. public SettUnit getSettUnitBySettUnitId(String systemId, String merchantId, String businessType) {  

key=#systemId+#merchantId+#businessType" 對象緩存的key值,需要保證唯一, 用的是Spring的 SpEL表達式, 取值爲 isbn對象的id屬性值

value="sysParamCache":指的是ehcache.xml裏的緩存名字

5、單元測試

[java] view plain copy
  1. @Test  
  2. public void getSettUnitBySettUnitIdTest() {  
  3.     String systemId = "CES";  
  4.     String merchantId = "133";  
  5.     SettUnit configSettUnit = settUnitService.getSettUnitBySettUnitId(systemId, merchantId, "ESP");  
  6.     SettUnit configSettUnit1 = settUnitService.getSettUnitBySettUnitId(systemId, merchantId, "ESP");  
  7.     boolean flag= (configSettUnit == configSettUnit1);  
  8.     System.out.println(configSettUnit);  
  9.     logger.info("查找結果" + configSettUnit.getBusinessType());  
  10.     
  11.   //  localSecondFIFOCache.put("configSettUnit", configSettUnit.getBusinessType());  
  12.  //  String string = localSecondFIFOCache.get("configSettUnit");  
  13.       logger.info("查找結果" + string);  
  14. }  

這是有緩存的結果,第二次直接從緩存中去取,比較兩個的地址,相等即表明兩上是同一個對象,第二次取的是第一次的結果


這是第一次取結果打印的SQL語句

發佈了81 篇原創文章 · 獲贊 91 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章