当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

求网站你们懂的2014,ww4399,边城小镇

注释介绍

@cacheable

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

@cacheable 作用和配置方法

参数 解释 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”)

实例

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

@cacheable(value="accountcache")// 使用了一个缓存名叫 accountcache 
public account getaccountbyname(string username) {
   // 方法内部实现不考虑缓存逻辑,直接实现业务
   system.out.println("real query account."+username); 
   return getfromdb(username); 
} 

@cacheput

@cacheput 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @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”)

实例

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

@cacheput(value="accountcache",key="#account.getname()")// 更新accountcache 缓存
public account updateaccount(account account) { 
  return updatedb(account); 
} 

@cacheevict

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

@cacheevict 作用和配置方法

参数 解释 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)

实例

@cacheevict(value="accountcache",key="#account.getname()")// 清空accountcache 缓存 
public void updateaccount(account account) {
   updatedb(account); 
} 

@cacheevict(value="accountcache",allentries=true)// 清空accountcache 缓存
public void reload() {
   reloadall()
}

@cacheable(value="accountcache",condition="#username.length() <=4")// 缓存名叫 accountcache 
public account getaccountbyname(string username) { 
 // 方法内部实现不考虑缓存逻辑,直接实现业务
 return getfromdb(username); 
}

@cacheconfig

所有的@cacheable()里面都有一个value=“xxx”的属性,这显然如果方法多了,写起来也是挺累的,如果可以一次性声明完 那就省事了, 所以,有了@cacheconfig这个配置,@cacheconfig is a class-level annotation that allows to share the cache names,如果你在你的方法写别的名字,那么依然以方法的名字为准。

@cacheconfig("books")
public class bookrepositoryimpl implements bookrepository {

  @cacheable
  public book findbook(isbn isbn) {...}
}

条件缓存

下面提供一些常用的条件缓存

//@cacheable将在执行方法之前( #result还拿不到返回值)判断condition,如果返回true,则查缓存; 
@cacheable(value = "user", key = "#id", condition = "#id lt 10")
public user conditionfindbyid(final long id) 

//@cacheput将在执行完方法后(#result就能拿到返回值了)判断condition,如果返回true,则放入缓存; 
@cacheput(value = "user", key = "#id", condition = "#result.username ne 'zhang'") 
public user conditionsave(final user user)  

//@cacheput将在执行完方法后(#result就能拿到返回值了)判断unless,如果返回false,则放入缓存;(即跟condition相反)
@cacheput(value = "user", key = "#user.id", unless = "#result.username eq 'zhang'")
public user conditionsave2(final user user)  

//@cacheevict, beforeinvocation=false表示在方法执行之后调用(#result能拿到返回值了);且判断condition,如果返回true,则移除缓存;
@cacheevict(value = "user", key = "#user.id", beforeinvocation = false, condition = "#result.username ne 'zhang'") 
public user conditiondelete(final user user)  

@caching

有时候我们可能组合多个cache注解使用;比如用户新增成功后,我们要添加id–>user;username—>user;email—>user的缓存;此时就需要@caching组合多个注解标签了。

@caching(put = {
@cacheput(value = "user", key = "#user.id"),
@cacheput(value = "user", key = "#user.username"),
@cacheput(value = "user", key = "#user.email")
})
public user save(user user) {

自定义缓存注解

比如之前的那个@caching组合,会让方法上的注解显得整个代码比较乱,此时可以使用自定义注解把这些注解组合到一个注解中,如:

@caching(put = {
@cacheput(value = "user", key = "#user.id"),
@cacheput(value = "user", key = "#user.username"),
@cacheput(value = "user", key = "#user.email")
})
@target({elementtype.method, elementtype.type})
@retention(retentionpolicy.runtime)
@inherited
public @interface usersavecache {
}

这样我们在方法上使用如下代码即可,整个代码显得比较干净。

@usersavecache
public user save(user user)

扩展

比如findbyusername时,不应该只放username–>user,应该连同id—>user和email—>user一起放入;这样下次如果按照id查找直接从缓存中就命中了

@caching(
  cacheable = {
    @cacheable(value = "user", key = "#username")
  },
  put = {
    @cacheput(value = "user", key = "#result.id", condition = "#result != null"),
    @cacheput(value = "user", key = "#result.email", condition = "#result != null")
  }
)
public user findbyusername(final string username) {
  system.out.println("cache miss, invoke find by username, username:" + username);
  for (user user : users) {
    if (user.getusername().equals(username)) {
      return user;
    }
  }
  return null;
}

其实对于:id—>user;username—->user;email—>user;更好的方式可能是:id—>user;username—>id;email—>id;保证user只存一份;如:

@cacheput(value="cachename", key="#user.username", cachevalue="#user.username") 
public void save(user user)  


@cacheable(value="cachename", key="#user.username", cachevalue="#caches[0].get(#caches[0].get(#username).get())") 
public user findbyusername(string username) 

spel上下文数据

spring cache提供了一些供我们使用的spel上下文数据,下表直接摘自spring官方文档:

名称 位置 描述 示例
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

@cacheevict(value = "user", key = "#user.id", condition = "#root.target.cancache() and #root.caches[0].get(#user.id).get().username ne #user.username", beforeinvocation = true) 
public void conditionupdate(user user) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网