千家信息网

Springboot Cache @CacheEvict无法模糊删除怎么办

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要讲解了"Springboot Cache @CacheEvict无法模糊删除怎么办",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Sprin
千家信息网最后更新 2026年11月01日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安全错误 数据库的锁怎样保障安全 南京欣网互联网络科技有限公司 互联网金融 金融科技 互联网科技公司发展 无线互联网科技 科技互联网公司logo 上海亿帐通互联网科技 互联网金融和金融科技 中正互联网科技招聘 成都仁贤领冠互联网科技有限公司 互联网实体化科技化 科技与互联网的利与弊 互联网时代科技改变生活 湖南一路畅行互联网科技 天津腾达互联网科技有限公司 上海京穆互联网科技有限公司 伍佰万互联网科技怎么赚钱 张召忠谈中国互联网科技 下一代互联网重大应用科技园 恒生科技互联网指数十大成分股 互联网科技企业文化 互联网巨头反垄断金融科技 深圳科技互联网 万易互联网科技有限公司 荆州市昕露互联网络科技有限公司 互联网金融科技展 互联网科技快讯热点 半导体是互联网高科技吗 济南小医互联网科技有限公司招聘 第三届互联网大会安防黑科技 广州全民伙伴互联网科技有限公司
0