记一次SpringBoot中@Cacheable不生效的解决

记一次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也一样,尽量设置生不可变的
在这里插入图片描述
问题解决,要是此博客对您有帮助点个赞,有问题评论区问我

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值