下面簡單介紹一下spring3.1.M1中的cache功能。
spring3.1.M1中負責cache的模組是org.springframework.context-3.1.0.M1.jar
與2.5時的modules模組類似,3.1的註解緩衝也是在方法上聲明註解,3.1同樣提供了兩個註解:
@Cacheable:負責將方法的傳回值加入到緩衝中
@CacheEvict:負責清除緩衝
@Cacheable支援如下幾個參數:
value:緩衝位置名稱,不可為空,如果使用EHCache,就是ehcache.xml中聲明的cache的name
key:緩衝的key,預設為空白,既表示使用方法的參數類型及參數值作為key,支援SpEL
condition:觸發條件,只有滿足條件的情況才會加入緩衝,預設為空白,既表示全部都加入緩衝,支援SpEL
例如:
Java代碼
- //將緩衝儲存進andCache,並使用參數中的userId加上一個字串(這裡使用方法名稱)作為緩衝的key
- @Cacheable(value="andCache",key="#userId + 'findById'")
- public SystemUser findById(String userId) {
- SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);
- return user ;
- }
- //將緩衝儲存進andCache,併當參數userId的長度小於32時才儲存進緩衝,預設使用參數值及類型作為緩衝的key
- @Cacheable(value="andCache",condition="#userId.length < 32")
- public boolean isReserved(String userId) {
- System.out.println("hello andCache"+userId);
- return false;
- }
@CacheEvict支援如下幾個參數:
value:緩衝位置名稱,不可為空,同上
key:緩衝的key,預設為空白,同上
condition:觸發條件,只有滿足條件的情況才會清除緩衝,預設為空白,支援SpEL
allEntries:true表示清除value中的全部緩衝,預設為false
例如:
Java代碼
- //清除掉指定key的緩衝
- @CacheEvict(value="andCache",key="#user.userId + 'findById'")
- public void modifyUserRole(SystemUser user) {
- System.out.println("hello andCache delete"+user.getUserId());
- }
-
- //清除掉全部緩衝
- @CacheEvict(value="andCache",allEntries=true)
- public final void setReservedUsers(String[] reservedUsers) {
- System.out.println("hello andCache deleteall");
- }
一般來說,我們的更新操作只需要重新整理緩衝中某一個值,所以定義緩衝的key值的方式就很重要,最好是能夠唯一,因為這樣可以準確的清除掉特定的緩衝,而不會影響到其它緩衝值,
比如我這裡針對使用者的操作,使用(userId+方法名稱)的方式設定key值,當然,你也可以找到更適合自己的方式去設定。
SpEL:Spring Expression Language
關於SpEL的介紹,可以參考如下地址:
http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/expressions.html
瞭解了cache的註解之後,接下來說說如何使註解生效,其實就是需要在spring的設定檔中增加一些配置。
1.spring-cache
首先我們來看一下如何使用spring3.1自己的cache,
需要在命名空間中增加cache的配置
Xml代碼
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
之後添加如下聲明:
Xml代碼
- <!-- 啟用緩衝註解功能,這個是必須的,否則註解不會生效,另外,該註解一定要聲明在spring主設定檔中才會生效 -->
- <cache:annotation-driven cache-manager="cacheManager"/>
-
-
- <!-- spring自己的換管理器,這裡定義了兩個緩衝位置名稱 ,既註解中的value -->
- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
- <property name="caches">
- <set>
- <bean
- class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
- p:name="default" />
- <bean
- class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
- p:name="andCache" />
- </set>
- </property>
- </bean>
2.spring-ehcache
接下來說說對ehcache的支援,其實只需要把cacheManager換成EHCache的cacheManager即可,如下:
Xml代碼
- <!-- 啟用緩衝註解功能,這個是必須的,否則註解不會生效,另外,該註解一定要聲明在spring主設定檔中才會生效 -->
- <cache:annotation-driven cache-manager="cacheManager"/>
-
- <!-- cacheManager工廠類,指定ehcache.xml的位置 -->
- <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
- p:configLocation="classpath:/config/ehcache.xml" />
-
- <!-- 聲明cacheManager -->
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
- p:cacheManager-ref="cacheManagerFactory" />
ehcache.xml
Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
- monitoring="autodetect">
- <!--
- <diskStore path="java.io.tmpdir" /> -->
- <diskStore path="E:/cachetmpdir"/>
- <defaultCache maxElementsInMemory="10000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
- maxElementsOnDisk="10000000" diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
-
- <cache name="andCache" maxElementsInMemory="10000"
- maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
- diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
- memoryStoreEvictionPolicy="LFU" />
- </ehcache>
ok,這樣註解緩衝就生效了。
附件中是我自己寫的一個小例子,工程結構如下所示,運行com.piaoyi.function.demo下的DemoTest即可
參考資料:
http://blog.springsource.com/2011/02/23/spring-3-1-m1-caching/
http://hi.baidu.com/coolcooldool/blog/item/3b541533c72b40e21a4cffda.html