Spring3.1 Cache注解

摘要:
--启用缓存注释功能,这是必需的,否则注释将不会生效。此外,注释必须在spring主配置文件中声明才能生效--˃˂!

@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>    

 

免责声明:文章转载自《Spring3.1 Cache注解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇KD-tree学习笔记(超全!)设置和取消环境变量下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

最佳内存缓存框架Caffeine

Caffeine是一种高性能的缓存库,是基于Java 8的最佳(最优)缓存框架。 Cache(缓存),基于Google Guava,Caffeine提供一个内存缓存,大大改善了设计Guava's cache 和 ConcurrentLinkedHashMap 的体验。 1 LoadingCache<Key, Graph> graphs = Ca...

fastapi之根据model生成schema和router

概述 fastapi没有对应的admin,所以在需要配置后端的时候,会比较麻烦,每个接口都需要自己手动写。 但是很多时候我们可能需要一个比较标准的东西,比如…一个装饰器 通过装饰器装饰model就可以自动注册路由,自动生成对应的标准方法,再根据更多的一些自定义配置,甚至不需要自己手动写schema,然后不需要自己单独写路由相关的东西。(相当于给sqlalc...

Web_0006:阿里云服务器OSS缓存设置清理

工作中的项目使用了前后端分离得方式去开发,前端存储在OSS上套了一层CDN做加速分发(也因为CDN的价格比OSS更低)。但是最近老是遇到前端部署页面后客户端显示炸了的问题。 搜索之后,发现OSS支持 对象更新后刷新CDN缓存的功能。 操作步骤 1.点击进入「对象存储OSS」2.左侧列表选择指定的bucket3.选择顶部tab栏的「域名管理」4.添加域名后配...

02-OpenLDAP配置

OpenLDAP配置 在OpenLDAP 2.4版本中,配置OpenLDAP的方法有两种:一种通过修改配置文件实现配置,另一种通过修改数据库的形式完成配置。 通过配置数据库完成各种配置,属于动态配置且不需要重新启动slapd进程服务。此配置数据库(cn=config)包含一个基于文本的集合LDIF文件(位于/etc/openldap/slapd.d目录下)...

Java并发机制和底层实现原理

  Java代码在编译后会变成Java字节码,字节码被类加载器加载到JVM里,JVM执行字节码转化为汇编指令在CPU上执行。Java中的并发机制依赖于JVM的实现和CPU的指令。      Java语言规范第三版中对volatile的定义如下:Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致的更新,线程应该确保通过排它锁单独获得这个变量...

nginx配置及性能调优

https://www.toutiao.com/i6765746230141125132/?timestamp=1575450096&app=news_article&group_id=6765746230141125132&req_id=201912041701360100260760263C04643 2.4、配置默认主页loc...