基于 Spring Aop 环绕通知实现 Redis 缓存双删功能(示例代码)

 更新时间:2022年08月16日 08:44:26   作者:风起风落丶  
基于 spring aop 常规应用场景多是用于日志记录以及实现 redis 分布式锁,在 github 中也有项目是把它拿来当作缓存的异常捕捉,这篇文章主要介绍了基于 Spring Aop 环绕通知实现 Redis 缓存双删,需要的朋友可以参考下

基于 spring aop 常规应用场景多是用于日志记录以及实现 redis 分布式锁,在 github 中也有项目是把它拿来当作缓存的异常捕捉。从而避免影响实际业务的开发;在某天,笔者有个业务开发是给某个服务模块增加 redis 缓存。增加缓存就会涉及 redis 删除。所以笔者就在思考是不是可以用环绕通知的方式来进行实现

代码实现

结构示意图:

自定义注解 RedisDelByDbUpdate

@Repeatable 表示允许在同一个地方上使用相同的注解,没有该注解时贴相同注解会报错

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(RedisDelByDbUpdateList.class)
public @interface RedisDelByDbUpdate {
    /**
     * 具体操作得缓存前缀
     */
    String redisPrefix() default "";
 
    /**
     * 具体的缓存名称,为空则删除 redisPrefix 下的所有缓存
     */
    String fieldName() default "";
}

自定义注解 RedisDelByUpdateList

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisDelByDbUpdateList {
    
    RedisDelByDbUpdate[] value();
}

具体实现 redis 双删逻辑

@Aspect
@Component
@Slf4j
public class RedisDelAspect_bak {
    //环绕增强
    @Autowired
    private RedisUtil redis;
 
    @Pointcut("@annotation(com.cili.baseserver.aop.annotation.RedisDelByDbUpdate) " +
            "|| @annotation(com.cili.baseserver.aop.annotation.RedisDelByDbUpdateList)")
    public void pointCut() {
 
    }
 
    // 线程池定长设置:最佳线程数目 = ((线程等待时间+线程CPU时间)/线程CPU时间 )* CPU数目
    ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(26);
 
    /**
     * 环绕增强
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
 
        RedisDelByDbUpdate redisDelByDbUpdate = method.getAnnotation(RedisDelByDbUpdate.class);
        if (redisDelByDbUpdate != null) {
            return singleRedisDel(redisDelByDbUpdate, point);
        }
 
        RedisDelByDbUpdateList updateList = method.getAnnotation(RedisDelByDbUpdateList.class);
        return arraysRedisDel(updateList, point);
    }
 
    private Object singleRedisDel(RedisDelByDbUpdate redisDelByDbUpdate, ProceedingJoinPoint point) throws Throwable {
        return handle(redisDelByDbUpdate, point, new IObjectCallback<Object>() {
            public <T> T callback() throws Throwable {
                return (T) point.proceed();
            }
        });
    }
 
    private Object arraysRedisDel(RedisDelByDbUpdateList updateList, ProceedingJoinPoint point) throws Throwable {
        RedisDelByDbUpdate[] redisDelByDbUpdates = updateList.value();
        for (int i = 0; i < redisDelByDbUpdates.length; i++) {
            boolean flag = i == redisDelByDbUpdates.length - 1 ? true : false;
 
            Object obj = handle(redisDelByDbUpdates[i], point, new IObjectCallback<Object>() {
                public <T> T callback() throws Throwable {
                    return flag ? (T) point.proceed() : null;
                }
            });
            if (flag) return obj;
        }
        return null;
    }
 
    private Object handle(RedisDelByDbUpdate redisDelByDbUpdate, ProceedingJoinPoint point,
                          IObjectCallback<Object> object) throws Throwable {
 
        String prefix = redisDelByDbUpdate.redisPrefix();
        String fieldName = redisDelByDbUpdate.fieldName();
 
        if (ValueUtil.isEmpty(prefix)) {
            log.info("redis缓存前缀不能为空");
            throw new BizException(BaseResponseCode.SYSTEM_BUSY);
        }
 
        Object arg = point.getArgs()[0];
        String key = "";
        String[] redisKeys = null;
 
        if (ValueUtil.isNotEmpty(fieldName)) {
            if (arg instanceof ArrayList) {
                redisKeys = ((ArrayList<?>) arg).stream().map(item -> prefix + item).toArray(String[]::new);
            } else {
                Map<String, Object> map = (Map<String, Object>) JsonUtil.toMap(JsonUtil.toJSON(point.getArgs()[0]));
                key = map.get(fieldName).toString();
            }
        } else {
            // 获取所有该前缀下缓存
            Set<String> keys = redis.keys(prefix + "*");
            redisKeys = keys.stream().toArray(String[]::new);
        }
 
        // 删除缓存
        String redisKey = prefix + key;
        delete(redisKey, redisKeys);
        Object result = object.callback();
 
        // 延时删除
        try {
            String[] finalRedisKeys = redisKeys;
            threadPool.schedule(new Runnable() {
                @Override
                public void run() {
                    delete(redisKey, finalRedisKeys);
                }
            }, 500, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            log.error("线程池延时删除缓存失败:{}", redisKey);
        }
        return result;
    }
 
    private void delete(String redisKey, String[] redisKeys) {
        if (ValueUtil.isEmpty(redisKeys)) {
            redis.delete(redisKey);
            return;
        }
        redis.del(redisKeys);
    }
}

注解使用示例

public class Test {
    @Override
    @RedisDelByDbUpdate(redisPrefix = RedisConstant.BANNER, fieldName = "ids")
    @RedisDelByDbUpdate(redisPrefix = RedisConstant.BANNER_LIST)
    public void batchDeleted(List<String> ids) throws BizException {
        if (CollectionUtils.isEmpty(ids)) {
            log.info("banner ID 不能为空");
            throw new BizException(BaseResponseCode.PARAM_IS_NOT_NULL);
        }
        try {
            bannerMapper.batchDeleted(ids);
        } catch (Exception e) {
            log.error("==>批量删除Banner错误:{}<==", e);
            throw new BizException(BaseResponseCode.SYSTEM_BUSY);
        }
    }
}

到此这篇关于基于 Spring Aop 环绕通知实现 Redis 缓存双删的文章就介绍到这了,更多相关Spring Aop Redis 缓存双删内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • redis加锁的几种方式汇总

    redis加锁的几种方式汇总

    这篇文章主要介绍了redis加锁的几种方式汇总,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Redis定时任务原理的实现

    Redis定时任务原理的实现

    本文主要是基于 redis 6.2 源码进行分析定时事件的数据结构和常见操作,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Redis MGET命令深度解析

    Redis MGET命令深度解析

    Redis的MGET命令是一种高效的批量读取操作,可以显著提高读取性能,减少网络往返的次数,本文从MGET命令的机制实现、底层原理、应用场景及性能优化等多个维度,深入解析Redis中的MGET命令的工作方式,并对它与其他批量操作命令的对比进行了详细介绍
    2024-09-09
  • Redis设置键的生存时间或过期时间的方法详解

    Redis设置键的生存时间或过期时间的方法详解

    这篇文章主要介绍了Redis如何设置键的生存时间或过期时间,通过EXPIRE命令或者PEXIPIRE命令,客户端可以以秒或者毫秒精度为数据库中的某个键设置生存时间,文中有详细的代码供供大家参考,需要的朋友可以参考下
    2024-03-03
  • Redis中切片集群详解

    Redis中切片集群详解

    切片集群Redis中,数据增多了,是该加内存还是加实例?采用云主机来运行Redis实例,那么,该如何选择云主机的内存容量呢?用Redis保存5000万个键值对,每个键值对大约是512B方案一:大内存云主机:选择一台32GB内存的云主机来部署Redis
    2025-01-01
  • Redis队列和阻塞队列的实现

    Redis队列和阻塞队列的实现

    本文主要介绍了Redis队列和阻塞队列的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • Redisson之lock()和tryLock()的区别及说明

    Redisson之lock()和tryLock()的区别及说明

    这篇文章主要介绍了Redisson之lock()和tryLock()的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Redis解决缓存雪崩、穿透和击穿的问题(Redis使用必看)

    Redis解决缓存雪崩、穿透和击穿的问题(Redis使用必看)

    这篇文章主要给大家介绍了Redis解决缓存雪崩、缓存穿透、缓存击穿的解决方案,文中有详细的图文介绍,具有一定的参考价值,需要的朋友可以参考下
    2023-08-08
  • Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

    Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

    在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能会遇到各种连接问题,其中“客户端IP不在白名单中”是一个常见的错误,本文将从错误分析、原因排查、解决方案等详细探讨如何解决这一问题
    2025-01-01
  • Redis基本数据类型List常用操作命令

    Redis基本数据类型List常用操作命令

    这篇文章主要为大家介绍了Redis数据类型List常用命令操作,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05

最新评论