Spring3.1 Cache注解

须要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,导致没法使用spring来方便的管理cache注解,好在3.1.M1中增长了对cache注解的支持,可喜可贺啊!html

 

但愿了解spring2.5的cache注解,能够参考以下内容:java

Spring基于注解的缓存配置--EHCache AND OSCache

Spring基于注解的缓存配置--web应用实例

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

 

例如:

Java代码  收藏代码

  1. //将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)做为缓存的key   

  2. @Cacheable(value="andCache",key="#userId + 'findById'")  

  3. public SystemUser findById(String userId) {  

  4.     SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);        

  5.     return user ;         

  6. }  

  7. //将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型做为缓存的key  

  8. @Cacheable(value="andCache",condition="#userId.length < 32")  

  9. public boolean isReserved(String userId) {  

  10.     System.out.println("hello andCache"+userId);  

  11.     return false;  

  12. }  

 

 

@CacheEvict 支持以下几个参数:

value:缓存位置名称,不能为空,同上

key:缓存的key,默认为空,同上

condition:触发条件,只有知足条件的状况才会清除缓存,默认为空,支持SpEL

allEntries:true表示清除value中的所有缓存,默认为false

 

例如:

Java代码  收藏代码

  1. //清除掉指定key的缓存  

  2. @CacheEvict(value="andCache",key="#user.userId + 'findById'")  

  3. public void modifyUserRole(SystemUser user) {  

  4.          System.out.println("hello andCache delete"+user.getUserId());  

  5. }  

  6.   

  7. //清除掉所有缓存  

  8. @CacheEvict(value="andCache",allEntries=true)  

  9. public void setReservedUsers() {  

  10.     System.out.println("hello andCache deleteall");  

  11. }  

 

通常来讲,咱们的更新操做只须要刷新缓存中某一个值,因此定义缓存的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代码  收藏代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"  

  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  

  3.      xmlns:cache="http://www.springframework.org/schema/cache"  

  4.     xsi:schemaLocation="  

  5.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  

  6.             http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">  

 

以后添加以下声明:

Xml代码  收藏代码

  1.       <!-- 启用缓存注解功能,这个是必须的,不然注解不会生效,另外,该注解必定要声明在spring主配置文件中才会生效 -->  

  2. <cache:annotation-driven cache-manager="cacheManager"/>  

  3.   

  4.   

  5. <!-- spring本身的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->  

  6. <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  

  7.     <property name="caches">  

  8.         <set>  

  9.             <bean  

  10.                 class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"  

  11.                 p:name="default" />  

  12.             <bean  

  13.                 class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"  

  14.                 p:name="andCache" />  

  15.         </set>  

  16.     </property>  

  17. </bean>   

 

2.spring-ehcache

接下来讲说对ehcache的支持,其实只须要把cacheManager换成EHCache的cacheManager便可,以下:

Xml代码  收藏代码

  1.        <!-- 启用缓存注解功能,这个是必须的,不然注解不会生效,另外,该注解必定要声明在spring主配置文件中才会生效 -->  

  2. <cache:annotation-driven cache-manager="cacheManager"/>  

  3.   

  4. <!-- cacheManager工厂类,指定ehcache.xml的位置 -->   

  5. <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"   

  6.     p:configLocation="classpath:/config/ehcache.xml" />   

  7.   

  8. <!-- 声明cacheManager -->  

  9. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"   

  10.     p:cacheManager-ref="cacheManagerFactory" />  

 

 

 ehcache.xml

Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  3.     xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  

  4.     monitoring="autodetect">  

  5.     <!--  

  6.     <diskStore path="java.io.tmpdir" /> -->  

  7.     <diskStore path="E:/cachetmpdir"/>  

  8.     <defaultCache maxElementsInMemory="10000" eternal="false"  

  9.         timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"  

  10.         maxElementsOnDisk="10000000" diskPersistent="false"  

  11.         diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />  

  12.           

  13.     <cache name="andCache" maxElementsInMemory="10000"  

  14.         maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"  

  15.         diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"  

  16.         memoryStoreEvictionPolicy="LFU" />  

  17. </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

相关文章
相关标签/搜索