@Cacheable 缓存注解的用法

来源:互联网 发布:阿尔法软件官方下载 编辑:程序博客网 时间:2024/05/01 12:22

 在spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式。不过,在实际开发中,我们往往是通过Spring的@Cacheable来实现数据的缓存的,所以,本文给大家详细介绍一下@Cacheable的用法。首先,在使用@Cacheable之前,我们要做好准备工作。

第一步:要导入相应的jar包。
   <classpathentry kind="lib" path="lib/spring-core-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-cache-1.0.10.jar"/>
    <classpathentry kind="lib" path="lib/spring-context-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-beans-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
    <classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
    <classpathentry kind="lib" path="lib/spring-expression-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/java_memcached-release_2.0.1.jar"/>
    <classpathentry kind="lib" path="lib/spring-aop-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-aspects-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-context-support-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-tx-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/aopalliance-1.0.jar"/>
    <classpathentry kind="lib" path="lib/ognl-3.0.6.jar"/>
    <classpathentry kind="lib" path="lib/trafficCounter-1.0.2.jar"/>
    <classpathentry kind="lib" path="lib/aspectjweaver-1.8.4.jar"/>
    <classpathentry kind="lib" path="lib/javassist-3.11.0.GA.jar"/>

第二步:xml文件中增加命名空间。

[html] view plain copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8.     http://www.springframework.org/schema/cache  
  9.     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
  10.     http://www.springframework.org/schema/context   
  11.     http://www.springframework.org/schema/context/spring-context-3.1.xsd"></span>  
  12.       
之后添加如下声明:

[html] view plain copy
  1. </pre><pre name="code" class="html"><span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8.     http://www.springframework.org/schema/cache  
  9.     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
  10.     http://www.springframework.org/schema/context   
  11.     http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  12.       
  13.     <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->   
  14.     <cache:annotation-driven cache-manager="cacheManager" />  
  15.       
  16.     <!-- cacheManager工厂类,指定ehcache.xml的位置 -->     
  17.     <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
  18.         <property name="configLocation"  value="classpath:cache/ehcache.xml"/>    
  19.     </bean>  
  20.       
  21.     <!-- 声明cacheManager -->    
  22.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
  23.         <property name="cacheManager" ref="cacheManagerFactory"/>  
  24.     </bean>  
  25. </beans></span>  
第二步: ehcache.xml

[html] view plain copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
  4.     updateCheck="false" dynamicConfig="false" monitoring="autodetect">    
  5.     <diskStore path="java.io.tmpdir" />  
  6.     <!--  
  7.         diskStore path:用来配置磁盘缓存使用的物理路径  
  8.         name:   缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)  
  9.         eternal="false"   元素是否永恒,如果是就永不过期(必须设置)  
  10.         maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大   
  11.         maxElementsInMemory="1000" 内存缓存中最多可以存放的元素数量(必须设置)  
  12.         timeToIdleSeconds="0"   导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0  
  13.         timeToLiveSeconds="600" 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期  
  14.         overflowToDisk="false"  当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)  
  15.         diskPersistent="false"  磁盘缓存在VM重新启动时是否保持(默认为false)  
  16.         diskExpiryThreadIntervalSeconds="100" 磁盘失效线程运行时间间隔,默认是120秒  
  17.         memoryStoreEvictionPolicy="LFU" 内存存储与释放策略.当达到maxElementsInMemory时  
  18.                共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)默认使用"最近使用"策略  
  19.     -->  
  20.     <defaultCache    
  21.         eternal="false"  
  22.         maxElementsInMemory="3000"    
  23.         timeToIdleSeconds="3600"    
  24.         timeToLiveSeconds="0"    
  25.         overflowToDisk="true"    
  26.         diskPersistent="false"    
  27.         diskExpiryThreadIntervalSeconds="100"    
  28.         memoryStoreEvictionPolicy="LRU"/>  
  29.   
  30.     <cache name="propConfigCache"    
  31.         eternal="false"  
  32.         maxElementsInMemory="3000"    
  33.         overflowToDisk="true"  
  34.         timeToIdleSeconds="0"    
  35.         timeToLiveSeconds="1440"   
  36.         memoryStoreEvictionPolicy="LFU"/>  
  37.           
  38.     <cache name="workTimeCache"    
  39.         eternal="false"  
  40.         maxElementsInMemory="3000"    
  41.         overflowToDisk="true"  
  42.         timeToIdleSeconds="0"    
  43.         timeToLiveSeconds="1440"   
  44.         memoryStoreEvictionPolicy="LFU"/>  
  45.       
  46.     <cache name="threeInOneCache"    
  47.         eternal="false"  
  48.         maxElementsInMemory="3000"    
  49.         overflowToDisk="true"  
  50.         timeToIdleSeconds="0"    
  51.         timeToLiveSeconds="1440"   
  52.         memoryStoreEvictionPolicy="LFU"/>  
  53.       
  54.     <cache name="transferCache"    
  55.         eternal="false"  
  56.         maxElementsInMemory="1000"    
  57.         overflowToDisk="true"  
  58.         timeToIdleSeconds="0"    
  59.         timeToLiveSeconds="1440"   
  60.         memoryStoreEvictionPolicy="LFU"/>  
  61.           
  62.     <cache name="threeInOneFavCache"    
  63.         eternal="false"  
  64.         maxElementsInMemory="3000"    
  65.         overflowToDisk="true"  
  66.         timeToIdleSeconds="0"    
  67.         timeToLiveSeconds="1440"   
  68.         memoryStoreEvictionPolicy="LFU"/>  
  69.           
  70.     <cache name="reserveTimeCache"    
  71.         eternal="false"  
  72.         maxElementsInMemory="3000"    
  73.         overflowToDisk="true"  
  74.         timeToIdleSeconds="0"    
  75.         timeToLiveSeconds="1440"   
  76.         memoryStoreEvictionPolicy="LFU"/>  
  77.           
  78.     <cache name="mqServerNameCache"    
  79.         eternal="false"  
  80.         maxElementsInMemory="3000"    
  81.         overflowToDisk="true"  
  82.         timeToIdleSeconds="0"    
  83.         timeToLiveSeconds="1440"   
  84.         memoryStoreEvictionPolicy="LFU"/>  
  85.           
  86.     <cache name="schWorkTimeCache"    
  87.         eternal="false"  
  88.         maxElementsInMemory="3000"    
  89.         overflowToDisk="true"  
  90.         timeToIdleSeconds="0"    
  91.         timeToLiveSeconds="1440"   
  92.         memoryStoreEvictionPolicy="LFU"/>  
  93.           
  94. </ehcache></span>  

@Cacheable 支持如下几个参数:

value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name

key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL

condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL

[java] view plain copy
  1. <span style="font-size:14px;">//将缓存保存进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. } </span>  

@CacheEvict 支持如下几个参数:

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

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

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

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

[java] view plain copy
  1. <span style="font-size:14px;">   //清除掉指定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.    }</span>    

一般来说,我们的更新操作只需要刷新缓存中某一个值,所以定义缓存的key值的方式就很重要,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值 ,

比如我这里针对用户的操作,使用(userId+方法名称)的方式设定key值 ,当然,你也可以找到更适合自己的方式去设定。


EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
Ehcache最初是由Greg Luck于2003年开始开发。2009年,该项目被Terracotta购买。软件仍然是开源,但一些新的主要功能(例如,快速可重启性之间的一致性的)只能在商业产品中使用,例如Enterprise EHCache and BigMemory。,维基媒体Foundationannounced目前使用的就是Ehcache技术。

主要的特性有:
1. 快速
2. 简单
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现

转载自:http://blog.csdn.net/qq_17555933/article/details/51320800

1 0
原创粉丝点击