缓存注解之@Cacheable、@CacheEvict、@CachePut

缓存注解之@Cacheable、@CacheEvict、@CachePut

Spring3.1版本及之后加入了对Cache的支持。

Spring Cache作用在方法上:

调用改注解标记的缓存方法时,会把该方法的参数和返回的结果值作为一个键值对(key-value)存入缓存中,下次传入同样的参数时调用该方法时,不会再次执行该方法,会直接从缓存中获取结果并返回。
注意:在使用Spring Cache时候需要保证缓存的方法对于传入相同的参数时,其返回的结果是相同的。

Spring Cache的配置方式:

  • 注解配置
  • xml配置

@Cacheable:

标记在一个类上时,表示该类下所有的方法都是支持缓存的。
标记在一个方法上时,表示该方法是支持缓存的。
当一个方法支持缓存时,Spring会在其方法被调用后将其方法的返回值放入缓存中,当再次传入相同的参数时可以从直接从缓存中获取结果。
Spring在缓存方法的返回值时是以key-value形式的键值对进行存取,键是默认策略和自定义策略来设置,值则是方法的返回值。

属性:

  • value:缓存的名称,在 spring 配置文件中定义,必须指定至少一个
    如:@Cacheable(value=”test01”) ,@Cacheable(value={”test01”,”test02”}

  • key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。 自定义策略则是指可以通过Spring的EL表达式来指定key
    如:@Cacheable(value=”test”,key=”#name”)

/**
* key :调用方法时传入时的参数
*
*/
   @Cacheable(value="users", key="#id")
   public User find(Integer id) {
      return null;
   }
   
  // 表示第一个参数
  @Cacheable(value="users", key="#p0")
   public User find(Integer id) {
      return null;
   }
   
  // 表示User中的id值
   @Cacheable(value="users", key="#user.id")
   public User find(User user) {
      return null;
   }
   
  // 表示第一个参数里的id属性值
   @Cacheable(value="users", key="#p0.id")
   public User find(User user) {
      return null;
   }

Spring中也可以通过一个root对象生成key:
root对象属性:

1.methodName:当前方法名 如 #root.methodName
2.method:当前方法 如 #root.method.name
3.target:当前被调用的对象 如 #root.target
4.targetClass:当前被调用的对象的class 如:#root.targetClass
5.args: 当前方法参数组成的数组 如:#root.args[0]
6.caches:当前被调用的方法使用的Cache 如:#root.caches[0].name
注:使用root对象的属性作为key时可以将“#root”省略

  // key值为: user中的name属性的值
  @Cacheable(value={"users", "xxx"}, key="caches[1].name")
   public User find(User user) {
      returnnull;
   }
  • condition:缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

    如:@Cacheable(value=”test”,condition=”#name.length()>2”)

    // 根据条件判断是否进行缓存
    @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==1")
   public User find(User user) {
      return user;
   }

@CachePut:

声明一个方法支持缓存功能,且每次调用时都会执行该方法,无论传入的参数是否与上一次相同。

   //@CachePut同样可以标注在类上和方法上,可以指定的属性跟@Cacheable是一样的。
   @CachePut("users")//每次都会执行方法,并将结果存入指定的缓存中
   public User find(Integer id) {
      return null;
   }

@CacheEvict:

标注在需要清除缓存元素的方法或类上
当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作

属性:

  • value :表示清除操作是发生在哪些Cache上的, 同@Cacheable对应的属性类似

  • key :表示需要清除的是哪个key, 同@Cacheable对应的属性类似

  • condition:表示清除操作发生的条件 ,同@Cacheable对应的属性类似

  • allEntries:boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。

  //清除users缓存中的所有的元素
   @CacheEvict(value="users", allEntries=true)
   public void delete(Integer id) {
      
   }
  • beforeInvocation:为true时,Spring会在调用该方法之前清除缓存中的指定元素。默认是在对应方法成功执行之后触发的。
   @CacheEvict(value="users", beforeInvocation=true)
   public void delete(Integer id) {
     int i = 1 / 0; //此处抛异常,但仍然清除缓存
   }

@Caching:

可以在一个方法或者类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict

   @Caching(cacheable = @Cacheable("user"), evict = { @CacheEvict("test01"),
   @CacheEvict(value = "test02", allEntries = true) })
   public User find(Integer id) {
      return null;
   }
发布了49 篇原创文章 · 获赞 40 · 访问量 3550

猜你喜欢

转载自blog.csdn.net/xueguchen/article/details/104423164