Ehcache整合spring配置

爲了提高系統的運行效率,引入緩存機制,減少數據庫訪問和磁盤IO。下面說明一下ehcache和spring整合配置。

1.   需要的jar包

slf4j-api-1.6.1.jar

ehcache-core-2.1.0.jar

ehcache-spring-annotations-1.1.2.jar

slf4j-log4j12-1.6.1.jar

spring-context-support-4.0.6.RELEASE.jar

2.   ehcache.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">  
  3.      
  4.     <diskStore path="java.io.tmpdir/ehcache"/>  
  5.       
  6.     <!-- 默認緩存 -->  
  7.     <defaultCache  
  8.            maxElementsInMemory="1000"  
  9.            eternal="false"  
  10.            timeToIdleSeconds="120"  
  11.            timeToLiveSeconds="120"  
  12.            overflowToDisk="false"/>  
  13.              
  14.     <!-- 菜單緩存 -->      
  15.     <cache name="menuCache"   
  16.            maxElementsInMemory="1000"   
  17.            eternal="false"  
  18.            timeToIdleSeconds="120"  
  19.            timeToLiveSeconds="120"  
  20.            overflowToDisk="false"   
  21.            memoryStoreEvictionPolicy="LRU"/>  
  22.       
  23. </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.   application_spring_cache.xml

[html] view plaincopy
  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"  
  4.     xmlns:context="http://www.springframework.org/schema/context"   
  5.     xmlns:cache="http://www.springframework.org/schema/cache"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.         http://www.springframework.org/schema/cache  
  11.         http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">  
  12.       
  13.     <cache:annotation-driven cache-manager="cacheManager"/>  
  14.       
  15.      <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  16.         <property name="configLocation" value="classpath:application/ehcache.xml" />  
  17.     </bean>  
  18.       
  19.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">      
  20.         <property name="cacheManager"  ref="cacheManagerFactory"/>      
  21.     </bean>  
  22.    
  23. </beans>  

4.   使用

首先在ehcache.xml中配置緩存策略,即添加一組cache。

4.1     添加緩存

[java] view plaincopy
  1. @Cacheable(value="menuCache",key="'UserMenuKey'+#userid")  
  2.     public List<MenuBean> queryMenuByUserId(String userid)  
  3. {  
  4. ……  
  5. }  

@Cacheable主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存。

value

緩存的名稱,在 spring 配置文件中定義,必須指定至少一個

例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}

key

緩存的 key,可以爲空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合

例如:
@Cacheable(value=” menuCache”,

key=”#userName”)

condition

緩存的條件,可以爲空,使用 SpEL 編寫,返回 true 或者 false,只有爲 true 才進行緩存

例如:
@Cacheable(value=” menuCache”,

condition=”#userName.length()>2”)

 

@CachePut主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存,和 @Cacheable 不同的是,它每次都會觸發真實方法的調用。

value

緩存的名稱,在 spring 配置文件中定義,必須指定至少一個

例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}

key

緩存的 key,可以爲空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合

例如:
@Cacheable(value=”testcache”,

key=”#userName”)

condition

緩存的條件,可以爲空,使用 SpEL 編寫,返回 true 或者 false,只有爲 true 才進行緩存

例如:
@Cacheable(value=” menuCache”,

condition=”#userName.length()>2”)

 

4.2     清除緩存

[java] view plaincopy
  1. @CacheEvict(value="menuCache",key="'UserMenuKey'+#userRoleBean.getUserId()")  
  2. public boolean delUserRole(UserRoleBean userRoleBean) {  
  3. ……  
  4. }  

@CachEvict主要針對方法配置,能夠根據一定的條件對緩存進行清空。

value

緩存的名稱,在 spring 配置文件中定義,必須指定至少一個

例如:
@CachEvict(value=” menuCache”) 或者 
@CachEvict(value={”cache1”,”cache2”}

key

緩存的 key,可以爲空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合

例如:
@CachEvict(value=” menuCache”,

key=”#userName”)

condition

緩存的條件,可以爲空,使用 SpEL 編寫,返回 true 或者 false,只有爲 true 才清空緩存

例如:
@CachEvict(value=” menuCache”,
condition=”#userName.length()>2”)

allEntries

是否清空所有緩存內容,缺省爲 false,如果指定爲 true,則方法調用後將立即清空所有緩存

例如:
@CachEvict(value=” menuCache”,

allEntries=true)

beforeInvocation

是否在方法執行前就清空,缺省爲 false,如果指定爲 true,則在方法還沒有執行的時候就清空緩存,缺省情況下,如果方法執行拋出異常,則不會清空緩存

例如:
@CachEvict(value=” menuCache”,beforeInvocation=true)

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章