1.自动生成key

  1. @Bean
  2. public KeyGenerator keyGenerator() {
  3. return new KeyGenerator() {
  4. @Override
  5. public Object generate(Object target, Method method, Object... params) {
  6. StringBuilder sb = new StringBuilder();
  7. sb.append(target.getClass().getName()+":");
  8. sb.append(method.getName()+":");
  9. for (Object obj : params) {
  10. sb.append(obj.toString());
  11. }
  12. return sb.toString();
  13. }
  14. };
  15. }

这个根据类名,方法名,参数组成

虽然自动生成key,但是基本不用,不太好控制

2.注解讲解

@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”)

@CachePut

@CachePut 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用

@CachePut主要用于 更新

@CacheEvict

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

@CacheConfig

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

@Caching

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

  1. @Caching(
  2. cacheable = {
  3. @Cacheable(value = "user", key = "#username")
  4. },
  5. put = {
  6. @CachePut(value = "user", key = "#result.id", condition = "#result != null"),
  7. @CachePut(value = "user", key = "#result.email", condition = "#result != null")
  8. }
  9. )
  10. public User findByUsername(final String username) {
  11. System.out.println("cache miss, invoke find by username, username:" + username);
  12. for (User user : users) {
  13. if (user.getUsername().equals(username)) {
  14. return user;
  15. }
  16. }
  17. return null;
  18. }

=============================================

困难的地方,就是key的描述,相关文章很少

Name Location Description Example
methodName root object The name of the method being invoked
  1. #root.methodName
method root object The method being invoked
  1. #root.method.name
target root object The target object being invoked
  1. #root.target
targetClass root object The class of the target being invoked
  1. #root.targetClass
args root object The arguments (as array) used for invoking the target
  1. #root.args[0]
caches root object Collection of caches against which the current method is executed
  1. #root.caches[0].name
argument name evaluation context Name of any of the method argument. If for some reason the names are not available (ex: no debug information), the argument names are also available under the a<#arg> where #arg stands for the argument index (starting from 0).
  1. iban

or

  1. a0

(one can also use

  1. p0

or p<#arg> notation as an alias).

代码总结;

  1. public static final String KEY = "cacheKey";

必须public

必须static,final

调用:

  1. @Override
  2. @Cacheable(value = "cacheName", key = "#root.target.KEY")
  3. public List<String> getCacheMethod() throws Exception{

更复杂的使用:

  1. public static final RedisKeyConstants redis=new RedisKeyConstants();

调用:

  1. @Cacheable(key="#root.target.redis.getApp_UserInfoManage_GetRoleList()+#request.getAccessToken()")

就是不知道怎么能对静态类进行调用

对root的使用:

  1. @Override
  2. @Cacheable(key="#root.targetClass.name+':'+#root.methodName+':'+#request.getLoanOrderId()")
  3. public DataResponse getLoanOrderDetail(@RequestBody @Validated QueryLoanDetailRequest request){

root.targetClass.name就是方法所在类的名称

root.methodName就是方法名

参考:

https://my.oschina.net/sdlvzg/blog/1608871

https://blog.csdn.net/whatlookingfor/article/details/51833378

https://www.xncoding.com/2017/07/28/spring/sb-cache.html

https://blog.csdn.net/l1028386804/article/details/70946410

http://jinnianshilongnian.iteye.com/blog/2001040

https://my.oschina.net/lis1314/blog/708711

https://docs.spring.io/spring/docs/3.2.0.RC1/reference/html/cache.html

https://www.jianshu.com/p/95ddef3168f8

Spring-Cache 注解 @Cacheable,@CachePut , @CacheEvict的更多相关文章

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

    https://blog.csdn.net/u012240455/article/details/80844361 注释介绍 @Cacheable @Cacheable 的作用 主要针对方法配置,能够 ...

  2. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用(转)

    原文地址:https://www.cnblogs.com/fashflying/p/6908028.html 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对 ...

  3. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  4. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  5. Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  6. 缓存注解@Cacheable、@CacheEvict、@CachePut使用及注解失效时间

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  7. spring Cache注解详解

    @CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置.在这里@CacheConfig(cacheNames = "users"):配置了该数据访问对象中返回的内容 ...

  8. spring Cache注解

    如下:不能将缓存注解加在listCate(boolean isShowHide)方法上 因为spring是使用AOP的方法获取缓存,在一个bean中再去调用别一个方法,不会应用缓存 @Cacheabl ...

  9. spring boot redis -> @Cacheable,@CacheEvict, @CachePut

    https://blog.csdn.net/eumenides_/article/details/78298088?locationNum=9&fps=1 https://www.cnblog ...

随机推荐

  1. yii2开启事务

    public function actionAdd() { $model = new Goods(); $model->setScenario('insert'); if ($model-> ...

  2. css选择问题

    <div class="col-lg-4 col-md-6 mb-4"> <div class="card"> <a href=& ...

  3. turtle库基础练习

    1.画一组同切圆 import turtle turtle.circle(10) turtle.circle(20) turtle.circle(30) turtle.circle(40) turtl ...

  4. 【2017-2-23】C#switch case分支语句,for循环语句

    switch case分支语句 switch(一个变量值) { case 值:要执行的代码段;break; case 值:要执行的代码段;break; … default:代码段;break;(def ...

  5. Lua逻辑操作符

    [1]逻辑操作符and.or和not 应用示例: ) ) -- nil ) -- false ) ) ) ) ) ) ) print(not nil) -- ture print(not false) ...

  6. Qt—MVC架构

    [1]代理应用示例源码 用代码说事,比较靠谱. 代码目录:三个自定义类,重实现QStyledItemDelegate类.main函数应用示例. (1)ComboDelegate.h #ifndef C ...

  7. flask 的类中间件

    需求 : 如果登陆了,就可以访问 index 和 home 页面,如果没登录就跳转到 login 登录 要怎么解决呢, session 对, 用 session 除了 Login 函数之外的所有函数里 ...

  8. 搭建Vue2+Vuex+Webpack+Pug(jade)+Stylus环境

     一.开发环境配置 开始之前,假设你已经安装了最新版本的 node 和 npm. 全局安装 vue-cli 和 webpack : npm install vue-cli webpack -g 创建工 ...

  9. The Little Prince-12/13

    The Little Prince-12/13 突然发现:这应该是一封情书~ 我那时什么也不懂!我应该根据她的行为,而不是根据她的话来判断她. 她使我的生活芬芳多彩,我真不该离开她跑出来.我本应该猜出 ...

  10. 通过 Java 线程堆栈进行性能瓶颈分析

    改善性能意味着用更少的资源做更多的事情.为了利用并发来提高系统性能,我们需要更有效的利用现有的处理器资源,这意味着我们期望使 CPU 尽可能出于忙碌状态(当然,并不是让 CPU 周期出于应付无用计算, ...