用@cacheevict删除缓存只能删除指定key的缓存,有些情况需要根据前缀删除所有key的时候,用@cacheevict就做不到了,所以我们自定义一个@cacheremove来处理根据前缀模糊删除所有cache(支持spring el表达式)
添加依赖
<dependency> <groupid>org.spri嘴唇起泡怎么办ngframework.boot</groupid> <artifactid>spring-boot-starter-aop</artifactid></dependency>
启动类加上 @enableaspectjautoproxy
@cacheremove 代码
package com.marssvn.utils.annotation.cache; import java.lang.annotation.retention;import java.lang.annotation.retentionpolicy;import java.lang.annotation.target; import static java.lang.annotation.elementtype.method; @target({method})@retention(retentionpoli孔子的小故事cy.runtime)public @interface cacheremove { string[] value() default {};}
cacheremoveaspect aop实现类代码
package com.marssvn.utils.annotation.cache.aspect; import com.marssvn.utils.annotation.cache.cacheremove;import org.aspectj.lang.joinpoint;import org.aspectj.lang.annotation.afterreturning;import org.aspectj.lang.annotation.aspect;import org.aspectj.lang.reflect.methodsignature;import org.slf4j.logger;import org.slf4j.loggerfactory;import org.springframework.core.localvariabletableparameternamediscoverer;import org.springframework.data.redis.core.stringredistemplate;import org.springframework.expression.expressionp晋级核潜艇arr;import org.springframework.expression.spel.standard.spelexpressionparr;import org.springframework.expression.spel.support.standardevaluationcontext;import org.springframework.stereotype.component; import javax.annotation.resource;import java.lang.reflect.method;import java.util.t; @aspect@componentpublic class cacheremoveaspect { @resource private strin柯达广告语gredistemplate stringredistemplate; private logger logger = loggerfactory.getlogger(this.getclass()); @afterreturning("@annotation(com.marssvn.utils.annotation.cache.cacheremove)") public void remove(joinpoint point) { method method = ((methodsignature) point.getsignature()).getmethod(); cacheremove cacheremove = method.getannotation(cacheremove.class); string[] keys = cacheremove.value(); for (string key : keys) { if (key.contains("#")) key = parkey(key, method, point.getargs()); t<string> deletekeys = stringredistemplate.keys(key); stringredistemplate.delete(deletekeys); logger.info("cache key: " + key + " deleted"); } } /** * parkey from spel */ private string parkey(string key, method method, object [] args){ localvariabletableparameternamediscoverer u = new localvariabletableparameternamediscoverer(); string[] paranamearr = u.getparameternames(method); expressionparr parr = new spelexpressionparr(); standardevaluationcontext context = new standardevaluationcontext(); for (int i = 0; i < paranamearr.length; i++) { context.tvariable(paranamearr[i], args[i]); } return parr.parexpression(key).getvalue(context, string.class); }}
rvice中的调用代码
孔子见罗雀者翻译 /** * delete repository * * @param id repositoryid */ @override @transactional @cacheremove({"repository.list::*", "'repository::id=' + #id", "'repository.tree::id=' + #id + '*'"}) public void deleterepositorybyid(int id) { // business code }
@cacheevict(cachenames = "likename" ,allentries=true)
allentries=true
开启全匹配cachenames
填写 模糊删除的name以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 09:10:44,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/5f55c4f0164592a44b34f811f120b3b7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Springboot Cache @CacheEvict 无法模糊删除的解决方案.doc
本文 PDF 下载地址:Springboot Cache @CacheEvict 无法模糊删除的解决方案.pdf
留言与评论(共有 0 条评论) |