`

spring 3.1中的cache小结

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

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

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

    return new Person(firstName, surname, age);
  }

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

    return new Person(firstName, surname, age);
  }
}


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

 
@Cacheable(value = "employee", key = "#surname")
  public Person findEmployeeBySurname(String firstName, String surname, int age) {

    return new Person(firstName, surname, age);
  }


  这里注意指定的缓存的是根据key=surename。也可以指定表达式
@Cacheable(value = "employee", condition = "#age < 25")
  public Person findEmployeeByAge(String firstName, String surname, int age) {

    return new Person(firstName, surname, age);
  }


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

@Test
  public void testCache() {

    Person employee1 = instance.findEmployee("John", "Smith", 22);
    Person employee2 = instance.findEmployee("John", "Smith", 22);

    assertEquals(employee1, employee2);
  }



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

    Person employee1 = instance.findEmployeeBySurname("John", "Smith", 22);
    Person employee2 = instance.findEmployeeBySurname("Jack", "Smith", 55);

    assertEquals(employee1, employee2);
  }


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

  继续单元测试:
 
@Test
  public void testCacheWithAgeAsCondition() {

    Person employee1 = instance.findEmployeeByAge("John", "Smith", 22);
    Person employee2 = instance.findEmployeeByAge("John", "Smith", 22);

    assertEquals(employee1, employee2);
  }



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


2 取消缓存
   下面看下如何取消缓存
  @CacheEvict
@CacheEvict(value = "employee", allEntries = true)
public void resetAllEntries() {

}

   使用@CacheEvict去取消缓存,

@CacheEvict支持如下几个参数:

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

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

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

allEntries:true表示清除value中的全部缓存,默认为false
  当然,也可以@cahceable和@cacheEvict一起使用,比如:
@CacheEvict(value = "employee", beforeInvocation = true)
@Cacheable(value = "employee")
public Person evictAndFindEmployee(String firstName, String surname, int age) {

  return new Person(firstName, surname, age);
}


  
@Test
public void testBeforeInvocation() {

  Person employee1 = instance.evictAndFindEmployee("John", "Smith", 22);
  Person employee2 = instance.evictAndFindEmployee("John", "Smith", 22);

  assertNotSame(employee1, employee2);
}

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

3 如何配置
   .spring-cache

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

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

beans xmlns="http://www.springframework.org/schema/beans"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
   xsi:schemaLocation="  
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
          http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">  



 <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->  
<cache:annotation-driven cache-manager="cacheManager"/>  
  
  
<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->  
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
    <property name="caches">  
       <set>  
          <bean  
               class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"  
                p:name="default" />  
            <bean  
                class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"  
                p:name="andCache" />  
       </set>      </property>  
</bean>   





   spring对ehcache并没有很好的支持,不建议使用,可以参看http://hanqunfeng.iteye.com/blog/1204343



8
2
分享到:
评论
3 楼 snowolf 2012-09-25  
jackyrong 写道
snowolf 写道
多个应用,分布式部署,缓存数据如何保持一致性?


感觉spring的cache是个鸡肋

如果把Memcached结合起来,获取能省掉我很多代码量!
2 楼 jackyrong 2012-09-24  
snowolf 写道
多个应用,分布式部署,缓存数据如何保持一致性?


感觉spring的cache是个鸡肋
1 楼 snowolf 2012-09-24  
多个应用,分布式部署,缓存数据如何保持一致性?

相关推荐

Global site tag (gtag.js) - Google Analytics