`
Fred_Han
  • 浏览: 144366 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring3.1 Cache注解

    博客分类:
  • WEB
 
阅读更多

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

 

希望了解spring2.5的cache注解,可以参考如下内容:

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

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

2.5时,spring没有自己的解决方案,都是采用对许多第三方cache框架的支持,比如EHCache和OSCache等等,不过到了3.1,spring就只提供EHCache的支持了,不过spring3.1还给出了自己的解决方案。

 

下面简单介绍一下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代码  收藏代码
  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 final void setReservedUsers(String[] reservedUsers) {  
  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,这样注解缓存就生效了。

转自:http://hanqunfeng.iteye.com/blog/1158824

分享到:
评论

相关推荐

    JAVA编程之spring cache本机缓存应用

    1、SpringCache是Spring提供的一个缓存框架,在Spring3.1版本开始支持将缓存添加到现有的spring应用程序中,在4.1开始,缓存已支持JSR-107注释和更多自定义的选项 2、Spring Cache利用了AOP,实现了基于注解的缓存...

    java之SpringCache之@Cacheable注解的说明使用

    在Spring3.1以后增加了一项happy的技术点,就是基于注解来实现缓存的技术。他的本质上不是具体的缓存方案实现,而是一个对缓存的抽象,让我们通过注解,实现少量的代码,达到实现缓存的方案。 Cacheable的说明 @...

    struts+spring+jdbc

    struts2.3.4+spring3.1.1+jdbcTemplate为了学习而搭建的小例子,里面用到了spring3.1原生的Cache,日志暂时没加入,整个例子全部使用注解

    SpringBoot2.0整合SpringCache和Redis.docx

    SpringBoot2.0整合SpringCache和Redis 1 1. SpringBoot对应(自带)RedisClient是不同的 1 2. 实战 1 2.1. maven配置 1 2.2. yml配置 1 2.3. RedisConfig配置 1 2.4. RedisController 2 3. Spring缓存注解@...

    springboot-cache.zip

    Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache(JSR-107)注解简化我们开发;大家一起来学习cache的用法

    springboot知识点整理

    学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 ...10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part2

    3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...

    JavaEE开发的颠覆者 Spring Boot实战,多个地址免费下载,

    3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...

    spring boot 项目代码,直接启动本人编写的,里面包含PPT下载地址

    3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part1

    3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...

    SpringMVC+Hibernate全注解整合

    注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --&gt; ...

    SpringBoot笔记-下篇.pdf

    Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义...

    JavaEE开发的颠覆者+Spring+Boot实战++完整版

    48 3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解...

    cms后台管理

    Jeecms是基于Spring注解,在自定义标签时对于实体类和dao service等注意注解的问题。 五 自定义标签及使用自己创建的表的实现过程 下面是我自己定义的标签mycontent_list 首先,在数据库里创建了一个jc_...

Global site tag (gtag.js) - Google Analytics