【redis】通过注解模糊匹配清除redis缓存

@CacheEvic(value="xx",key="xxx")只能单一删除

但是这样添加缓存时,会产生很多key,会根据不同的参数生成多条缓存数据,之久导致的当出现数据更新的时候更新缓存的困难。

 解决方案就是根据前缀或者后缀去清除redis缓存。

写一个注解,根据注解指定的key去模糊匹配redis缓存。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 功能描述:需要清除的key的模糊匹配缓存
 *
 * @author jasin
 * DATE 2019/9/25.
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheRemove {

	/**
	 * 需要清除的大类(模糊匹配) 例如 dictTableCache 所有缓存
	 *
	 * @return
	 */
	String value() default "";


	/**
	 * 需要清除的具体的key类型(模糊匹配)
	 *
	 * @return
	 */
	String[] key() default {};

}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * 功能描述:清除缓存切面类
 *
 * @author jasin
 * DATE 2019/9/25.
 */
@Component
@Aspect
public class CacheRemoveAspect {

    Logger logger = LoggerFactory.getLogger(this.getClass());

//    @Resource(name = "redisTemplate")
//    RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //截获标有@CacheRemove的方法
    @Pointcut(value = "@annotation(org.jeecg.common.aspect.annotation.CacheRemove)")
    private void pointcut() {
    }

    /**
     * 功能描述: 切面在截获方法返回值之后
     *
     * @return void
     * @throws
     * @author fuyuchao
     * @date 2018/9/14 16:55
     * @params [joinPoint]
     */
    @AfterReturning(value = "pointcut()")
    private void process(JoinPoint joinPoint) {
        //获取被代理的类
        Object target = joinPoint.getTarget();
        //获取切入方法的数据
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入方法
        Method method = signature.getMethod();
        //获得注解
        CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);

        if (cacheRemove != null) {

            //清除当前类的缓存
//            cleanRedisCache("*" + target.getClass().toString() + "*");

            String value = cacheRemove.value();
            if (!"".equals(value)) {
                //清除缓存的项目所有redis业务部缓存
                cleanRedisCache("*" + value + "*");
            }
            //需要移除的正则key
            String[] keys = cacheRemove.key();
            for (String key : keys) {
                //清除指定清除的key的缓存
                cleanRedisCache("*" + key + "*");
            }
        }
    }

    private void cleanRedisCache(String key) {
        if (key != null) {
            Set<String> stringSet = redisTemplate.keys(key);
            redisTemplate.delete(stringSet);//删除缓存
            logger.info("清除 " + key + " 缓存");
        }
    }

}

在编辑的方法或新增的方法加上注解

即可删除掉对应的redis的key。 

发布了110 篇原创文章 · 获赞 36 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/wjx_jasin/article/details/101394103
今日推荐