Spring之Ehcache缓存注解

一、@Cacheable(value,key,condition)

1.说明

主要针对方法配置,能够根据方法的请求参数对其结果进行缓存

参数 解释 example
value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
@Cacheable(value=”mycache”)
@Cacheable(value={”cache1”,”cache2”}
key 缓存方法的返回结果时对应的key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @Cacheable(value=”testcache”,key=”#userName”)
condition 缓存发生条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 @Cacheable(value=”testcache”,condition=”#userName.length()>2”)

2.实例 

当调用下面方法的时候,会从一个名叫 usersCache 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象。这里的缓存中的 key 就是参数 name,value 就是 User对象。“usersCache”缓存是在 spring*.xml 中定义的名称。

   @Cacheable(value="usersCache")
   public User find(String name) {

      return getFromDB(name);
   }

二、@CachePut(value,key,condition)

1.说明

主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,与@Cacheable不同的是使用@CachePut标注的方法在执行前,不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

参数 解释 example
value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 @CachePut(value=”my cache”)
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @CachePut(value=”testcache”,key=”#userName”)
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 @CachePut(value=”testcache”,condition=”#userName.length()>2”)

2.实例 

@CachePut 注释,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新

   @CachePut(value="usersCache")//每次都会执行方法,并将结果存入指定的缓存中
   public User find(String name) {

      return getFromDB(name);
   }

三、@CacheEvict(value,key,condition,allEntries,beforeInvocation)

1.说明

主要针对方法配置,能够根据一定的条件对缓存进行清空

参数 解释 example
value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 @CacheEvict(value=”my cache”)
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @CacheEvict(value=”testcache”,key=”#userName”)
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 @CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
allEntries 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 @CachEvict(value=”testcache”,allEntries=true)
beforeInvocation 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存 @CachEvict(value=”testcache”,beforeInvocation=true)

2.实例

   //清除缓存中的所有元素
   @CacheEvict(value="users", allEntries=true)
   public void delete(Integer id) {

      System.out.println("delete user by id: " + id);

   }


    //清除操作默认是在对应方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时也不会触发清除操作
  @CacheEvict(value="users", beforeInvocation=true)
   public void delete(Integer id) {

      System.out.println("delete user by id: " + id);

   }

四、@Caching(cacheable,put,evict) 

主要可以让我们在一个方法或者类上同时指定多个Spring Cache相关的注解

  @Caching(cacheable = @Cacheable("users"), evict = { @CacheEvict("cache2"),
         @CacheEvict(value = "cache3", allEntries = true) })
   public User find(Integer id) {

      return null;

   }

SpEL上下文数据

名称 位置 描述 示例
methodName root对象 当前被调用的方法名 root.methodName
method root对象 当前被调用的方法 root.method.name
target root对象 当前被调用的目标对象 root.target
targetClass root对象 当前被调用的目标对象类 root.targetClass
args root对象 当前被调用的方法的参数列表 root.args[0]
caches root对象 当前方法调用使用的缓存列表(如@Cacheable(value={“cache1”, “cache2”})),则有两个cache root.caches[0].name
argument name 执行上下文 当前被调用的方法的参数,如findById(Long id),我们可以通过#id拿到参数 user.id
result 执行上下文 方法执行后的返回值(仅当方法执行之后的判断有效,如‘unless','cache evict'的beforeInvocation=false) result

猜你喜欢

转载自blog.csdn.net/mmake1994/article/details/85635684