一次排查@CacheEvict注解失效的经历及解决_第1页
一次排查@CacheEvict注解失效的经历及解决_第2页
一次排查@CacheEvict注解失效的经历及解决_第3页
一次排查@CacheEvict注解失效的经历及解决_第4页
一次排查@CacheEvict注解失效的经历及解决_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一次排查@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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论