Springboot Cache @CacheEvict 无法模糊删除的解决方案

网友投稿 813 2022-09-05


Springboot Cache @CacheEvict 无法模糊删除的解决方案

目录SpringbootCache @CacheEvict 无法模糊删除以下代码适用于Redis@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.annhttp://otation.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;

importSPEjAmxCx 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

SPEjAmxCx 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++) {

http:// 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

看源码可知


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Scrapy框架中如何更改下载中间件里的headers?(scrapy框架怎么安装)
下一篇:盘点一道Pandas中分组聚合groupby()函数用法的基础题(pandas数据分组与聚合)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~