版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一次排查@CacheEvict注解失效的经历及解决publicObjectinvoke(finalMethodInvocationinvocation)throwsThrowable{
Methodmethod=invocation.getMethod();
CacheOperationInvokeraopAllianceInvoker=()-{
try{
returnceed();
catch(Throwableex){
thrownewCacheOperationInvoker.ThrowableWrapper(ex);
try{
returnexecute(aopAllianceInvoker,invocation.getThis(),method,invocation.getArguments());
catch(CacheOperationInvoker.ThrowableWrapperth){
throwth.getOriginal();
进入execute方法
protectedObjectexecute(CacheOperationInvokerinvoker,Objecttarget,Methodmethod,Object[]args){
//Checkwhetheraspectisenabled(tocopewithcaseswheretheAJispulledinautomatically)
if(this.initialized){
ClasstargetClass=getTargetClass(target);
CacheOperationSourcecacheOperationSource=getCacheOperationSource();
if(cacheOperationSource!=null){
CollectionCacheOperationoperations=cacheOperationSource.getCacheOperations(method,targetClass);
if(!CollectionUtils.isEmpty(operations)){
returnexecute(invoker,method,
newCacheOperationContexts(operations,method,args,target,targetClass));
returninvoker.invoke();
cacheOperationSource记录系统中所有使用了缓存的方法,cacheOperationSource.getCacheOperations(method,targetClass)能获取deleteByTaskId()方法缓存元数据,然后执行execute()方法
@Nullable
privateObjectexecute(finalCacheOperationInvokerinvoker,Methodmethod,CacheOperationContextscontexts){
//Specialhandlingofsynchronizedinvocation
if(contexts.isSynchronized()){
CacheOperationContextcontext=contexts.get(CacheableOperation.class).iterator().next();
if(isConditionPassing(context,CacheOperationExpressionEvaluator.NO_RESULT)){
Objectkey=generateKey(context,CacheOperationExpressionEvaluator.NO_RESULT);
Cachecache=context.getCaches().iterator().next();
try{
returnwrapCacheValue(method,cache.get(key,()-unwrapReturnValue(invokeOperation(invoker))));
catch(Cache.ValueRetrievalExceptionex){
//TheinvokerwrapsanyThrowableinaThrowableWrapperinstancesowe
//canjustmakesurethatonebubblesupthestack.
throw(CacheOperationInvoker.ThrowableWrapper)ex.getCause();
else{
//Nocachingrequired,onlycalltheunderlyingmethod
returninvokeOperation(invoker);
//Processanyearlyevictions
processCacheEvicts(contexts.get(CacheEvictOperation.class),true,
CacheOperationExpressionEvaluator.NO_RESULT);
//Checkifwehaveacacheditemmatchingtheconditions
Cache.ValueWrappercacheHit=findCachedItem(contexts.get(CacheableOperation.class));
//Collectputsfromany@Cacheablemiss,ifnocacheditemisfound
ListCachePutRequestcachePutRequests=newLinkedList();
if(cacheHit==null){
collectPutRequests(contexts.get(CacheableOperation.class),
CacheOperationExpressionEvaluator.NO_RESULT,cachePutRequests);
ObjectcacheValue;
ObjectreturnValue;
if(cacheHit!=nullcachePutRequests.isEmpty()!hasCachePut(contexts)){
//Iftherearenoputrequests,justusethecachehit
cacheValue=cacheHit.get();
returnValue=wrapCacheValue(method,cacheValue);
else{
//Invokethemethodifwedon'thaveacachehit
returnValue=invokeOperation(invoker);
cacheValue=unwrapReturnValue(returnValue);
//Collectanyexplicit@CachePuts
collectPutRequests(contexts.get(CachePutOperation.class),cacheValue,cachePutRequests);
//Processanycollectedputrequests,eitherfrom@CachePutora@Cacheablemiss
for(CachePutRequestcachePutRequest:cachePutRequests){
cachePutRequest.apply(cacheValue);
//Processanylateevictions
processCacheEvicts(contexts.get(CacheEvictOperation.class),false,cacheValue);
returnreturnValue;
这里大致过程是:
先执行beforInvokeEvict----执行数据库delete操作---执行CachePut操作----执行afterInvokeEvict
我们的注解是方法调用后再使缓存失效,直接所以有效的操作应在倒数第2行
privatevoidperformCacheEvict(
CacheOperationContextcontext,CacheEvictOperationoperation,@NullableObjectresult){
Objectkey=null;
for(Cachecache:context.getCaches()){
if(operation.isCacheWide()){
logInvalidating(context,operation,null);
doClear(cache);
else{
if(key==null){
key=generateKey(context,result);
logInvalidating(context,operation,key);
doEvict(cache,key);
这里通过context.getCaches()获取到name为taskParamsCache的缓存
然后generateKey生成key,注意这里,发现生成的key是com.xxx.xxx.atomic.impl.xxxxdeleteByTaskId982,但是缓存中的key却是com.xxx.xxx.atomic.impl.xxxxselectByTaskId982,下面调用的doEvict(cache,key)方法不再跟进了,就是从cache中移除key对应值。明显这里key对应不上的,这也是导致@CacheEvict没有生效的原因。
小结一下
我还是太大意了,当时看了注解@CacheEvict的对key的注释:
大意就是如果没有指定key,那就会使用方法所有参数生成一个key,明显com.xxx.xxx.atomic.impl.xxxxselectByTaskId982是方法名+参数,可是你没说把方法名还加上了啊,说好的只用参数呢,哈哈,这个bug是我使用不当引出的,很多人不会犯这种低级错误。
解决办法就是使用SpEL明确定义key
@Cacheable(value="taskParamsCache",key="#taskId")
ListTaskParamsselectByTaskId(LongtaskId);
//...
//...
@CacheEvict(value="taskParamsCache",key="#taskId")
intdeleteByTaskId(LongtaskId);
说说spring全家桶中@CacheEvict无效情况
@CacheEvict(value=“test”,allEntries=true)
1、使用@CacheEvict注解的方法必须是controller层直接调用,service里间接调用不生效。
2、原因是因为key值跟你查询方法的key值不统一,所以导致缓存并没有清除
3、把@CacheEvict的方法和@Cache的方法放到一个java文件中写,他俩在两个java文件的话,会导致@CacheEvict失效。
4、返回值必须设置为void
@CacheEvictannotation
Itisimportanttonotethatvoidmethodscanbeusedwith@CacheEvict
5、@CacheEvict必须作用在走代理的方法上
在使用Spring@CacheEvict注解的时候,要注意,如
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业安全生产奖惩制度
- 体检中心奖惩制度范本
- 保安及时关门奖惩制度
- 成都市人力资源和社会保障局 所属事业单位2026年上半年公开考试招聘工作人员(7人)考试备考试题及答案解析
- 2026浙江省丽水市莲都区人力社保局招聘8人考试备考题库及答案解析
- 2027中广核联合南华大学培养招聘考试备考题库及答案解析
- 2026中铝(重庆)铝材销售有限公司副总经理岗位招聘1人考试备考试题及答案解析
- 跨境电商行业2025年海外营销中心跨境电商技术创新可行性报告
- 2026云南昭通彝良县医共体总医院校园招聘编外专业技术人员44人笔试参考题库及答案解析
- 威海市2024年山东威海市环翠区民兵训练基地公开招聘工作人员3人笔试历年参考题库典型考点附带答案详解
- 2026年安徽财贸职业学院单招职业适应性测试题库带答案详解
- 2025年公开选拔副科级领导干部面试题及答案
- 2026年春季学期升旗仪式安排表及讲话稿(18周):春风作序开新卷步步生花向远方
- 2026年无锡工艺职业技术学院单招综合素质考试题库附答案解析
- 新苏教版科学二年级下册第3课《 四季的天气》教学课件
- 2025年智慧消防工程师专业技能实操考核要求试题及真题
- (2025年)胎心监护判读及处理试题及答案
- 深度解析(2026)《WJT 9102-2023 民爆专用生产设备通 用安全技术条件》
- 公共卫生足浴管理制度
- 2026年黑龙江能源职业学院单招职业适应性测试题库及答案1套
- 2026 年初中英语《名词》专项练习与答案 (100 题)
评论
0/150
提交评论