Springboot Cache @CacheEvict 无法模糊删除的解决方案
更新时间:2021年12月31日 11:12:06 作者:头秃小可爱
这篇文章主要介绍了Springboot Cache @CacheEvict 无法模糊删除的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
SpringbootCache @CacheEvict 无法模糊删除
用@CacheEvict删除缓存只能删除指定key的缓存,有些情况需要根据前缀删除所有key的时候,用@CacheEvict就做不到了,所以我们自定义一个@CacheRemove来处理根据前缀模糊删除所有cache(支持Spring EL表达式)
以下代码适用于Redis
添加依赖
<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根据缓存名称模糊删除
@CacheEvict(cacheNames = "likename" ,allEntries=true)
allEntries=true开启全匹配cacheNames填写 模糊删除的name
看源码可知

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java设计模式之抽象工厂模式AbstractFactoryPattern详解
这篇文章主要介绍了Java设计模式之抽象工厂模式AbstractFactoryPattern详解,抽象工厂模式是一种软件开发设计模式,抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来,需要的朋友可以参考下2023-10-10
Apache Commons fileUpload实现文件上传之一
这篇文章主要介绍了Apache Commons fileUpload实现文件上传之一的相关资料,需要的朋友可以参考下2016-03-03
手把手带你分析SpringBoot自动装配完成了Ribbon哪些核心操作
这篇文章主要介绍了详解Spring Boot自动装配Ribbon哪些核心操作的哪些操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-08-08
SpringBoot+Mybatis使用Mapper接口注册的几种方式
本篇博文中主要介绍是Mapper接口与对应的xml文件如何关联的几种姿势,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-07-07


最新评论