須要感慨一下,spring3.0時丟棄了2.5時的spring-modules-cache.jar,導致沒法使用spring來方便的管理cache註解,好在3.1.M1中增長了對cache註解的支持,可喜可賀啊!html
但願瞭解spring2.5的cache註解,能夠參考以下內容:java
2.5時,spring沒有本身的解決方案,都是採用對許多第三方cache框架的支持,好比EHCache和OSCache等等,不過到了3.1,spring就只提供EHCache的支持了,不過spring3.1還給出了本身的解決方案。web
下面簡單介紹一下spring3.1.M1中的cache功能。spring
spring3.1.M1中負責cache的模塊是org.springframework.context-3.1.0.M1.jarexpress
與2.5時的modules模塊相似,3.1的註解緩存也是在方法上聲明註解,3.1一樣提供了兩個註解:緩存
@Cacheable:負責將方法的返回值加入到緩存中框架
@CacheEvict:負責清除緩存spa
@Cacheable 支持以下幾個參數:xml
value:緩存位置名稱,不能爲空,若是使用EHCache,就是ehcache.xml中聲明的cache的namehtm
key:緩存的key,默認爲空,既表示使用方法的參數類型及參數值做爲key,支持SpEL
condition:觸發條件,只有知足條件的狀況纔會加入緩存,默認爲空,既表示所有都加入緩存,支持SpEL
例如:
//將緩存保存進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
例如:
//清除掉指定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 void setReservedUsers() {
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的配置
<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">
以後添加以下聲明:
<!-- 啓用緩存註解功能,這個是必須的,不然註解不會生效,另外,該註解必定要聲明在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便可,以下:
<!-- 啓用緩存註解功能,這個是必須的,不然註解不會生效,另外,該註解必定要聲明在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 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