springboot cache(Redis) 根据key*批量模糊删除缓存

原创文章,欢迎转载:http://miao.blog/article/springboot-cache-redis-fuzzy-delete

用@CacheEvict删除缓存只能删除指定key的缓存,有些情况需要根据前缀删除所有key的时候,用@CacheEvict就做不到了,所以我们自定义一个@CacheRemove来处理根据前缀模糊删除所有cache(支持Spring EL表达式)


以下代码适用于Redis

@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
    }

这样就可以通过前缀*模糊删除所有符合条件的缓存了。

此外,Springboot使用AOP还需要以下设置

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>


启动类加上 @EnableAspectJAutoProxy



over

推荐阅读
使用springboot框架对请求参数验证时,实现不同国家区域返回对应语言的消息内容
评论
ss0xt

ss0xt

1个月前

stringRedisTemplate.keys() 这个线上用会导致redis 耗时很久,redis又是单线程的会容易被锁住,严重一点会导致redis服务器宕机