1.自动生成key

    @Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName()+":");
sb.append(method.getName()+":");
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}

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

虽然自动生成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组合多个注解标签了。

@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;
}

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

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

Name Location Description Example
methodName root object The name of the method being invoked
#root.methodName
method root object The method being invoked
#root.method.name
target root object The target object being invoked
#root.target
targetClass root object The class of the target being invoked
#root.targetClass
args root object The arguments (as array) used for invoking the target
#root.args[0]
caches root object Collection of caches against which the current method is executed
#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).
iban

or

a0

(one can also use

p0

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

代码总结;

public static final String KEY = "cacheKey";

必须public

必须static,final

调用:

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

更复杂的使用:

public static final RedisKeyConstants redis=new RedisKeyConstants();

调用:

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

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

对root的使用:

    @Override
@Cacheable(key="#root.targetClass.name+':'+#root.methodName+':'+#request.getLoanOrderId()")
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. netframework转core时文件响应流问题

    做将framework webapi项目转成netcore平台上的webapi项目时,发现原来的返回文件响应流在netcore平台下失效.代码如下,返回pdf文件响应流,供前端显示 /// <s ...

  2. C. Primes or Palindromes?

    prime numbers non greater than n is about . We can also found the amount of palindrome numbers with ...

  3. sitecore系列教程之简单和个性化

    现代Web开发倾向于关注内容管理系统(CMS)的功能丰富的程序.最终用户可以做什么?作为内容管理者,我们可以为最终用户实现其目标提供哪些功能?开发人员可以为内容管理员构建哪些组件来实现它们? 相关内容 ...

  4. Lua 判断表是否为空方法

    [1]判断表为空的方法 目前为止,Lua语言中判断table表是否为空有三种方式: (1)#table,当table为数组时直接返回table表的长度. (2)当table是字典时,返回table的长 ...

  5. tomcat 、NIO、netty 本质

    tomcat 基于 Socket,面向 web 浏览器的通信容器 nio 同步非阻塞的I/O模型 netty 通信框架,对 nio 的封装

  6. Presto 学习

    Presto 基础知识与概念学习可以参考这些博客: presto 0.166概述 https://www.cnblogs.com/sorco/p/7060166.html Presto学习-prest ...

  7. RocketMQ最佳实战

    RocketMQ 客户端最佳实践 1. Producer最佳实践 发送消息注意事项 1). 一个应用尽可能用一个Topic,消息子类型用tags来标识,tags可以由应用自由设置. 2). 消息发送成 ...

  8. 大数据学习路线:Hadoop集群同步技术分享

    今天给大家带来的技术分享是——Hadoop集群同步. 一.同步方式 选择一个机器,作为时间服务器(这里选择hadoop01),所有的机器与这台集群时间进行定时的同步,比如,每隔十分钟,同步一次时间. ...

  9. web项目错误—Java.util.ConcurrentMidificationException

    源代码: Iterator<String> iterator = list.iterator(); synchronized(synObject) { while(iterator.has ...

  10. MyEclipse配置默认自带的Java代码格式化

    1.配置注释一大段代码块后,格式化代码,已经注释的代码仍然保持原样 (1)首先,Window -> Prefrences -> Java -> Code Style -> Fo ...