Spring-Cache 注解 @Cacheable,@CachePut , @CacheEvict
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). |
ibanor a0(one can also use p0or 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2016-07-04 MongoDB的C#封装类
2013-07-04 Linq let Concat