`

spring 3.1中的cache小结

 
阅读更多

spring 3.1中有cache了,下面结合目前网上的一些资料和手册的归纳总结下: 

1 @cache注解 
   在3.1中,都是用注解的了, 
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。 
 

Java代码   收藏代码
  1. @Cacheable(value = "employee")  
  2. public class EmployeeDAO {  
  3.   
  4.   public Person findEmployee(String firstName, String surname, int age) {  
  5.   
  6.     return new Person(firstName, surname, age);  
  7.   }  
  8.   
  9.   public Person findAnotherEmployee(String firstName, String surname, int age) {  
  10.   
  11.     return new Person(firstName, surname, age);  
  12.   }  
  13. }  


   在上面的代码中,缓存了Person了,命名为employee,缓存的是方法的值, 
@Cacheable注解有三个参数,value是必须的,还有key和condition。第一个参数,也就是value指明了缓存将被存到什么地方。 
    在spring 3.1中,可以使用spel表达式去进行缓存的指定,比如: 

 

Java代码   收藏代码
  1. @Cacheable(value = "employee", key = "#surname")  
  2.   public Person findEmployeeBySurname(String firstName, String surname, int age) {  
  3.   
  4.     return new Person(firstName, surname, age);  
  5.   }  


  这里注意指定的缓存的是根据key=surename。也可以指定表达式 

Java代码   收藏代码
  1. @Cacheable(value = "employee", condition = "#age < 25")  
  2.   public Person findEmployeeByAge(String firstName, String surname, int age) {  
  3.   
  4.     return new Person(firstName, surname, age);  
  5.   }  


   这里指定age<25的才缓存; 
  接下来看下如何应用,比如: 

Java代码   收藏代码
  1. @Test  
  2.   public void testCache() {  
  3.   
  4.     Person employee1 = instance.findEmployee("John""Smith"22);  
  5.     Person employee2 = instance.findEmployee("John""Smith"22);  
  6.   
  7.     assertEquals(employee1, employee2);  
  8.   }  


     这个时候肯定是相等的了,因为用的是缓存。但是如果调用的是 
findEmployeeBySurname方法的话,就一定有点不同了, 

Java代码   收藏代码
  1. @Test  
  2.   public void testCacheOnSurnameAsKey() {  
  3.   
  4.     Person employee1 = instance.findEmployeeBySurname("John""Smith"22);  
  5.     Person employee2 = instance.findEmployeeBySurname("Jack""Smith"55);  
  6.   
  7.     assertEquals(employee1, employee2);  
  8.   }  


  但由于是缓存的是根据surename为key,所以上面结果两个对象却依然是相等的(尽管原本看上去是不同的对象了),所以key的选择一定要小心。 

  继续单元测试: 
 

Java代码   收藏代码
  1. @Test  
  2.   public void testCacheWithAgeAsCondition() {  
  3.   
  4.     Person employee1 = instance.findEmployeeByAge("John""Smith"22);  
  5.     Person employee2 = instance.findEmployeeByAge("John""Smith"22);  
  6.   
  7.     assertEquals(employee1, employee2);  
  8.   }  


    这两个就一样了,因为都是age<25的,都缓存了,指向同一个对象。 


2 取消缓存 
   下面看下如何取消缓存 
  @CacheEvict 

Java代码   收藏代码
  1. @CacheEvict(value = "employee", allEntries = true)  
  2. public void resetAllEntries() {  
  3.   
  4. }  


   使用@CacheEvict去取消缓存, 

@CacheEvict支持如下几个参数: 

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

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

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

allEntries:true表示清除value中的全部缓存,默认为false 
  当然,也可以@cahceable和@cacheEvict一起使用,比如: 

Java代码   收藏代码
  1. @CacheEvict(value = "employee", beforeInvocation = true)  
  2. @Cacheable(value = "employee")  
  3. public Person evictAndFindEmployee(String firstName, String surname, int age) {  
  4.   
  5.   return new Person(firstName, surname, age);  
  6. }  


  

Java代码   收藏代码
  1. @Test  
  2. public void testBeforeInvocation() {  
  3.   
  4.   Person employee1 = instance.evictAndFindEmployee("John""Smith"22);  
  5.   Person employee2 = instance.evictAndFindEmployee("John""Smith"22);  
  6.   
  7.   assertNotSame(employee1, employee2);  
  8. }  


   这里的话,先使用@CacheEvict(value = "employee", beforeInvocation = true), 
会先清掉所有缓存,所以asset的结果就不相等了; 
  

3 如何配置 
   .spring-cache 

首先我们来看一下如何使用spring3.1自己的cache, 

需要在命名空间中增加cache的配置 

Java代码   收藏代码
  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">    



Java代码   收藏代码
  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>      </property>    
  16. </bean>     

 

 

 

@Cacheable(value = "andCache", key ="#surname",condition = "#age < 25 and #firstName=='two' ")  

public Person findEmployeeByAge(String firstName, String surname, int age) {  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics