您好,登录后才能下订单哦!
这篇文章主要讲解了“Springboot Cache @CacheEvict无法模糊删除怎么办”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Springboot Cache @CacheEvict无法模糊删除怎么办”吧!
用@CacheEvict删除缓存只能删除指定key的缓存,有些情况需要根据前缀删除所有key的时候,用@CacheEvict就做不到了,所以我们自定义一个@CacheRemove来处理根据前缀模糊删除所有cache(支持Spring EL表达式)
添加依赖
<dependency> <groupId>org.springframework.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(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 @Component public 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<String> 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.parseExpression(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(cacheNames = "likename" ,allEntries=true)
allEntries=true
开启全匹配
cacheNames
填写 模糊删除的name
感谢各位的阅读,以上就是“Springboot Cache @CacheEvict无法模糊删除怎么办”的内容了,经过本文的学习后,相信大家对Springboot Cache @CacheEvict无法模糊删除怎么办这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。