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

看源碼可知

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: