记一次SpringBoot中@Cacheable不生效的解决
标签: 缓存 springboot java
记一次SpringBoot中@Cacheable不生效的解决
最近做项目遇到一个性能瓶颈,数据库两张表100多万数据级联查询,非常慢,特别是涉及到分页这块,自己用explain查看执行计划,由于条件很多,加索引优化也没生效,故使用缓存的方式解决,但有一个缓存一直不生效,自己看了一下源码打断点看执行方法,最终解决了,记录一下我的方法,希望对你有用(最后发现主要问题是我两个方法使用同一个map作为key,导致后面key覆盖了前面key所以一直取不到正确的数据)
首先找到spring-context的jar包,找到里面的CacheInterceptor类,找到里面的invoke初始化方法
可以看到这里最下面调用了excute方法执行,点进去,到CacheAspectSupport类中,这个时候看excute
直接点到return里面的execute方法,这个事初化主方法,基本查找和存取都在这个里面
@Nullable
private Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) {
// Special handling of synchronized invocation
if (contexts.isSynchronized()) {
CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();
if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) {
Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
Cache cache = context.getCaches().iterator().next();
try {
return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker))));
}
catch (Cache.ValueRetrievalException ex) {
// The invoker wraps any Throwable in a ThrowableWrapper instance so we
// can just make sure that one bubbles up the stack.
throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause();
}
}
else {
// No caching required, only call the underlying method
return invokeOperation(invoker);
}
}
// Process any early evictions
processCacheEvicts(contexts.get(CacheEvictOperation.class), true,
CacheOperationExpressionEvaluator.NO_RESULT);
// Check if we have a cached item matching the conditions
Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));
// Collect puts from any @Cacheable miss, if no cached item is found
List<CachePutRequest> cachePutRequests = new LinkedList<>();
if (cacheHit == null) {
collectPutRequests(contexts.get(CacheableOperation.class),
CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);
}
Object cacheValue;
Object returnValue;
if (cacheHit != null && !hasCachePut(contexts)) {
// If there are no put requests, just use the cache hit
cacheValue = cacheHit.get();
returnValue = wrapCacheValue(method, cacheValue);
}
else {
// Invoke the method if we don't have a cache hit
returnValue = invokeOperation(invoker);
cacheValue = unwrapReturnValue(returnValue);
}
// Collect any explicit @CachePuts
collectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests);
// Process any collected put requests, either from @CachePut or a @Cacheable miss
for (CachePutRequest cachePutRequest : cachePutRequests) {
cachePutRequest.apply(cacheValue);
}
// Process any late evictions
processCacheEvicts(contexts.get(CacheEvictOperation.class), false, cacheValue);
return returnValue;
}
这个是它的源码,首先是判断了是否是同步,同步机制会特殊处理,但这里基本上都是异步,
processCacheEvicts这个方法基本上调用都是空,我这边打了好几次断点无论有没有缓存都是直接过的,里面的for循环里面,传入的contexts一直是空
按照它自己这个注解// Process any early evictions 处理任何早期驱逐应该是做一个初始判断,
接下来的findCachedItem为主方法,这个里面会根据contexts找到对应的key,再根据key去找到对应的值,这个方法具体的话有兴趣的朋友可以自己去看看,代码非常多,就不细聊了
Object key = generateKey(context, result); —>Cache.ValueWrapper cached = findInCaches(context, key);
主要是这两句获取到对应的缓存数据,
if (cacheHit != null && !hasCachePut(contexts)) {
// If there are no put requests, just use the cache hit
cacheValue = cacheHit.get();
returnValue = wrapCacheValue(method, cacheValue);
}
else {
// Invoke the method if we don't have a cache hit
returnValue = invokeOperation(invoker);
cacheValue = unwrapReturnValue(returnValue);
}
主方法往下走就是去判断是否取到缓存值,取到了就直接get,没取到回去执行具体方法(也就是会走数据库查询),同时把数据存到缓存中,我这边打两个断点,进去看一下数据情况
第一次进去(无缓存的情况)
name为我的缓存名称,可以看到是没有值的,我们把代码走到最后再次查看
这个时候可以看到已经存值进去了,第一次无缓存查询存储完毕,刷新进入第二次
这里看到缓存的数据key值明显不对,第一次是{{}=3},当时这个地方找了很久,基本每个子方法都走完了,也没找到原因,即使把所有别的缓存都去掉也没用,后面发现是参数的问题,如果你的缓存方法中是有参数的(无参的Key是SimpleKey={{}}这种格式的),有参的方法这个值一般在整个方法中需要保持一致,我的是因为我用的map作为参数存储,这个查询完后对又往map中添加了分页参数,导致这个缓存中的map值被改变了,当然具体为什么我都map更改了会影响之前存的key我后面会再研究下,找到了这个解决方法就很简单,把这个查询的map独立出来就可以了,如果不是map是别的值String或int也一样,尽量设置生不可变的
问题解决,要是此博客对您有帮助点个赞,有问题评论区问我
智能推荐
CentOS学习之路1-wget下载安装配置
参考1: https://blog.csdn.net/zhaoyanjun6/article/details/79108129 参考2: http://www.souvc.com/?p=1569 CentOS学习之路1-wget下载安装配置 1.wget的安装与基本使用 安装wget yum 安装软件 默认安装保存在/var/cache/yum ,用于所有用户使用。 帮助命令 基本用法 例子:下载...
深入浅出Spring的IOC容器,对Spring的IOC容器源码进行深入理解
文章目录 DispatcherServlet整体继承图 入口:DispatcherServlet.init() HttpServletBean.init() FrameworkServlet.initServletBean() 首先大家,去看Spring的源码入口,第一个就是DispatcherServlet DispatcherServlet整体继承图 入口:DispatcherServlet....
laravel框架的课堂知识点概总
1. MVC 1.1 概念理解 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑 MVC 是一种使用 MVC(Model View Controller ...
Unity人物角色动画系统学习总结
使用动画系统控制人物行走、转向、翻墙、滑行、拾取木头 混合树用来混合多个动画 MatchTarget用来匹配翻墙贴合墙上的某一点,人物以此为支点翻墙跳跃 IK动画类似于MatchTarget,控制两只手上的两个点来指定手的旋转和位置,使得拾取木头时更逼真 创建AnimatorController: 首先创建一个混合树,然后双击 可以看到该混合树有五种状态机,分别是Idle、WalkForward、...
Composer 安装 ThinkPHP6 问题
Composer 安装 ThinkPHP6 问题 先说说问题 一.运行环境要求 二.配置 参考: ThinkPHP6.0完全开发手册 先说说问题 执行ThinkPHP6的安装命令 遇到问题汇总如下: 看提示是要更新版本,执行命令更新。 更新之后,再次安装ThinkPHP,之后遇到如下问题。 尝试了很多方法,依然不能解决。其中包括使用https://packagist.phpcomposer.com...
猜你喜欢
Spring Boot 整合JDBC
今天主要讲解一下SpringBoot如何整合JDBC,没啥理论好说的,直接上代码,看项目整体结构 看一下对应的pom.xml 定义User.java 定义数据源配置,这里使用druid,所以需要写一个配置类 上面指定druid的属性配置,和用户登录的账号信息以及对应的过滤规则: 下面定义数据访问接口和对应的实现: 数据访问层很简单,直接注入JdbcTemplate模板即可,下面再看对应的servi...
html鼠标悬停显示样式
1.显示小手: 在style中添加cursor:pointer 实现鼠标悬停变成小手样式 实例: 其他参数: cursor语法: cursor : auto | crosshair | default | hand | move | help | wait | tex...
Yupoo(又拍网)的系统架构
Yupoo!(又拍网) 是目前国内最大的图片服务提供商,整个网站构建于大量的开源软件之上。以下为其使用到的开源软件信息: 操作系统:CentOS、MacOSX、Ubuntu 服务器:Apache、Nginx、Squid 数据库:MySQLmochiweb、MySQLdb 服务器监控:Cacti、Nagios、 开发语言:PHP、Python、Erlang、Java、Lua 分布式计算:Hadoop...
创建一个Servlet项目流程(入门)
版本 IDEA 2020.2 JDK1.8 apache-tomcat-9.0.36 项目流程 一、IDEA中新建JaveEE项目 项目起名,选择项目存放地址,点击finish创建成功 进入项目后,右键选择项目,选择add Framework Support 选择Web Application,点击OK 此时项目文件夹 在WEB-INF下创建两个目录classes和lib 按ctrl+alt+sh...
Docker部署SpringCloud ELK+RabbitMQ日志
Docker部署SpringCloud ELK+RabbitMQ日志 Im_Coder 原文:https://www.jianshu.com/p/f773f23096a9 一、效果图 image.png 二、ELK是什么? ELK由ElasticSearch、Logstash和Kiabana三个开源工具组成。 其中Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,索...