Springboot Cache @CacheEvict无法模糊删除怎么办
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要讲解了"Springboot Cache @CacheEvict无法模糊删除怎么办",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Sprin
千家信息网最后更新 2025年11月08日Springboot Cache @CacheEvict无法模糊删除怎么办
这篇文章主要讲解了"Springboot Cache @CacheEvict无法模糊删除怎么办",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Springboot Cache @CacheEvict无法模糊删除怎么办"吧!
SpringbootCache @CacheEvict 无法模糊删除
用@CacheEvict删除缓存只能删除指定key的缓存,有些情况需要根据前缀删除所有key的时候,用@CacheEvict就做不到了,所以我们自定义一个@CacheRemove来处理根据前缀模糊删除所有cache(支持Spring EL表达式)
以下代码适用于Redis
添加依赖
org.springframework.boot spring-boot-starter-aop
启动类加上 @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(RetentionPolicy.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.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.expression.spel.support.StandardEvaluationContext;import org.springframework.stereotype.Component; import javax.annotation.Resource;import java.lang.reflect.Method;import java.util.Set; @Aspect@Componentpublic class CacheRemoveAspect { @Resource private StringRedisTemplate 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 = parseKey(key, method, point.getArgs()); Set deleteKeys = stringRedisTemplate.keys(key); stringRedisTemplate.delete(deleteKeys); logger.info("cache key: " + key + " deleted"); } } /** * parseKey from SPEL */ private String parseKey(String key, Method method, Object [] args){ LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] paraNameArr = u.getParameterNames(method); ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); for (int i = 0; i < paraNameArr.length; i++) { context.setVariable(paraNameArr[i], args[i]); } return parser.parse_Expression(key).getValue(context, String.class); }} Service中的调用代码
/** * 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根据缓存名称模糊删除
@CacheEvict(cacheNames = "likename" ,allEntries=true)
allEntries=true开启全匹配cacheNames填写 模糊删除的name
看源码可知

感谢各位的阅读,以上就是"Springboot Cache @CacheEvict无法模糊删除怎么办"的内容了,经过本文的学习后,相信大家对Springboot Cache @CacheEvict无法模糊删除怎么办这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
怎么办
代码
缓存
学习
内容
前缀
情况
名称
就是
思路
文章
时候
更多
源码
知识
知识点
篇文章
表达式
跟着
问题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
后台密码忘了怎么更改数据库
网络安全和信息化龙头股
如何搭建iis服务器
pdrr网络安全模型对比
常见的数据库提权方法有
数据库金额脱敏
开启访客网络安全么
creo如服务器管理在哪
定向士官计算机网络技术
大学生学校校园网络安全宣传片
网络安全事件责任主要方式
网络安全国际学术会议
怎么看电脑服务器和数据库名称
电脑打印服务器没有开启
四川应用软件开发服务
安徽速聘优选网络技术有限公司
魔兽世界怀旧服icc服务器
网络安全都有什么方向
余姚敏捷软件开发平台
电脑网络技术有哪些类型
互联网大厂科技实习
tftp服务器程序
校园网络安全感想
健康网络安全宣传周
数据库加一列
青岛云触网络技术 近邻
中国网络安全法 saas
西城区信息化软件开发客户至上
武汉有什么软件开发
网络安全都有什么方向