• SpringBoot AOP + Redis 延时双删功能实战


    一、业务场景

    在多线程并发情况下,假设有两个数据库修改请求,为保证数据库与redis的数据一致性,修改请求的实现中需要修改数据库后,级联修改Redis中的数据。

    • 请求一:A修改数据库数据 B修改Redis数据

    • 请求二:C修改数据库数据 D修改Redis数据

    并发情况下就会存在A —> C —> D —> B的情况

    一定要理解线程并发执行多组原子操作执行顺序是可能存在交叉现象的

    1、此时存在的问题

    A修改数据库的数据最终保存到了Redis中,C在A之后也修改了数据库数据。

    此时出现了Redis中数据和数据库数据不一致的情况,在后面的查询过程中就会长时间去先查Redis, 从而出现查询到的数据并不是数据库中的真实数据的严重问题。

    2、解决方案

    在使用Redis时,需要保持Redis和数据库数据的一致性,最流行的解决方案之一就是延时双删策略。

    注意:要知道经常修改的数据表不适合使用Redis,因为双删策略执行的结果是把Redis中保存的那条数据删除了,以后的查询就都会去查询数据库。所以Redis使用的是读远远大于改的数据缓存。

    延时双删方案执行步骤

    1. 删除缓存

    2. 更新数据库

    3. 延时500毫秒 (根据具体业务设置延时执行的时间)

    4. 删除缓存

    3、为何要延时500毫秒?

    这是为了我们在第二次删除Redis之前能完成数据库的更新操作。假象一下,如果没有第三步操作时,有很大概率,在两次删除Redis操作执行完毕之后,数据库的数据还没有更新,此时若有请求访问数据,便会出现我们一开始提到的那个问题。

    4、为何要两次删除缓存?

    如果我们没有第二次删除操作,此时有请求访问数据,有可能是访问的之前未做修改的Redis数据,删除操作执行后,Redis为空,有请求进来时,便会去访问数据库,此时数据库中的数据已是更新后的数据,保证了数据的一致性。

    二、代码实践

    1、引入Redis和SpringBoot AOP依赖

    <dependency>  
          <groupId>org.springframework.boot</groupId>  
          <artifactId>spring-boot-starter-data-redis</artifactId>  
    </dependency>  
      
    <dependency>  
          <groupId>org.springframework.boot</groupId>  
          <artifactId>spring-boot-starter-aop</artifactId>  
    </dependency>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、编写自定义aop注解和切面

    ClearAndReloadCache延时双删注解

    `*/**  
     *延时双删  
     **/*  
    @Retention(RetentionPolicy.RUNTIME)  
    @Documented  
    @Target(ElementType.METHOD)  
    public@interface ClearAndReloadCache {  
    String name()default "";  
    }  
    `
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ClearAndReloadCacheAspect延时双删切面

    `@Aspect  
    @Component  
    publicclassClearAndReloadCacheAspect{  
      
    @Autowired  
    private StringRedisTemplate stringRedisTemplate;  
      
    */**  
    * 切入点  
    *切入点,基于注解实现的切入点  加上该注解的都是Aop切面的切入点  
    *  
    */*  
      
    @Pointcut("@annotation(com.pdh.cache.ClearAndReloadCache)")  
    publicvoidpointCut(){  
      
    }  
    */**  
    * 环绕通知  
    * 环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。  
    * 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型  
    * @param proceedingJoinPoint  
    */*  
    @Around("pointCut()")  
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint){  
        System.out.println("----------- 环绕通知 -----------");  
        System.out.println("环绕通知的目标方法名:" + proceedingJoinPoint.getSignature().getName());  
      
        Signature signature1 = proceedingJoinPoint.getSignature();  
        MethodSignature methodSignature = (MethodSignature)signature1;  
        Method targetMethod = methodSignature.getMethod();*//方法对象*  
        ClearAndReloadCache annotation = targetMethod.getAnnotation(ClearAndReloadCache.class);*//反射得到自定义注解的方法对象*  
      
        String name = annotation.name();*//获取自定义注解的方法对象的参数即name*  
        Set<String> keys = stringRedisTemplate.keys("*" + name + "*");*//模糊定义key*  
        stringRedisTemplate.delete(keys);*//模糊删除redis的key值*  
      
    *//执行加入双删注解的改动数据库的业务 即controller中的方法业务*  
        Object proceed = null;  
    try {  
            proceed = proceedingJoinPoint.proceed();  
        } catch (Throwable throwable) {  
            throwable.printStackTrace();  
        }  
      
    *//开一个线程 延迟1秒(此处是1秒举例,可以改成自己的业务)*  
    *// 在线程中延迟删除  同时将业务代码的结果返回 这样不影响业务代码的执行*  
    new Thread(() -> {  
    try {  
                Thread.sleep(1000);  
                Set<String> keys1 = stringRedisTemplate.keys("*" + name + "*");*//模糊删除*  
                stringRedisTemplate.delete(keys1);  
                System.out.println("-----------1秒钟后,在线程中延迟删除完毕 -----------");  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }).start();  
      
    return proceed;*//返回业务代码的值*  
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    `

    3、application.yml

    
    server:  
      port: 8082  
      
    spring:  
      # redis setting  
      redis:  
        host: localhost  
        port: 6379  
      
      # cache setting  
      cache:  
        redis:  
          time-to-live: 60000 # 60s  
      
      datasource:  
        driver-class-name: com.mysql.cj.jdbc.Driver  
    url: jdbc:mysql://localhost:3306/test  
    username: root  
    password: 1234  
      
    
      
    > 基于 SpringCloudAlibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能  
    >  
    > * 项目地址:<https://github.com/YunaiV/yudao-cloud>  
    > * 视频教程:<https://doc.iocoder.cn/video/>  
      
    # mp setting  
    mybatis-plus:  
      mapper-locations: classpath*:com/pdh/mapper/*.xml  
      global-config:  
        db-config:  
          table-prefix:  
      configuration:  
        # log of sql  
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  
        # hump  
        map-underscore-to-camel-case: true  
    `
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    4、user_db.sql脚本

    用于生产测试数据

    ``DROP TABLE IF EXISTS `user_db`;  
    CREATE TABLE `user_db`  (  
      `id` int(4) NOT NULL AUTO_INCREMENT,  
      `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,  
      PRIMARY KEY (`id`) USING BTREE  
    ) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;  
      
    -- ----------------------------  
    -- Records of user_db  
    -- ----------------------------  
    INSERT INTO `user_db` VALUES (1, '张三');  
    INSERT INTO `user_db` VALUES (2, '李四');  
    INSERT INTO `user_db` VALUES (3, '王二');  
    INSERT INTO `user_db` VALUES (4, '麻子');  
    INSERT INTO `user_db` VALUES (5, '王三');  
    INSERT INTO `user_db` VALUES (6, '李三');  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ``

    5、UserController

    /**  
     * 用户控制层  
     */*  
    @RequestMapping("/user")  
    @RestController  
    public class UserController {  
        @Autowired  
        private UserService userService;  
      
        @GetMapping("/get/{id}")  
        @Cache(name = "get method")  
        *//@Cacheable(cacheNames = {"get"})*  
        public Result get(@PathVariable("id") Integer id){  
            return userService.get(id);  
        }  
      
        @PostMapping("/updateData")  
        @ClearAndReloadCache(name = "get method")  
        public Result updateData(@RequestBody User user){  
            return userService.update(user);  
        }  
      
        @PostMapping("/insert")  
        public Result insert(@RequestBody User user){  
            return userService.insert(user);  
        }  
      
        @DeleteMapping("/delete/{id}")  
        public Result delete(@PathVariable("id") Integer id){  
            return userService.delete(id);  
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    6、UserService

    /**  
     * service层  
     */*  
    @Service  
    public class UserService {  
      
        @Resource  
        private UserMapper userMapper;  
      
        public Result get(Integer id){  
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();  
            wrapper.eq(User::getId,id);  
            User user = userMapper.selectOne(wrapper);  
            return Result.success(user);  
        }  
      
        public Result insert(User user){  
            int line = userMapper.insert(user);  
            if(line > 0)  
                return Result.success(line);  
            return Result.fail(888,"操作数据库失败");  
        }  
      
        public Result delete(Integer id) {  
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();  
            wrapper.eq(User::getId, id);  
            int line = userMapper.delete(wrapper);  
            if (line > 0)  
                return Result.success(line);  
            return Result.fail(888, "操作数据库失败");  
        }  
      
        public Result update(User user){  
            int i = userMapper.updateById(user);  
            if(i > 0)  
                return Result.success(i);  
            return Result.fail(888,"操作数据库失败");  
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    `

    三、测试验证

    1、ID=10,新增一条数据

    图片

    2、第一次查询数据库,Redis会保存查询结果

    图片

    3、第一次访问ID为10

    图片

    4、第一次访问数据库ID为10,将结果存入Redis

    图片

    5、更新ID为10对应的用户名(验证数据库和缓存不一致方案)

    图片

    数据库和缓存不一致验证方案:

    打个断点,模拟A线程执行第一次删除后,在A更新数据库完成之前,另外一个线程B访问ID=10,读取的还是旧数据。

    在这里插入图片描述

    图片

    6、采用第二次删除,根据业务场景设置延时时间,两次删除缓存成功后,Redis结果为空。读取的都是数据库真实数据,不会出现读缓存和数据库不一致情况。

    图片

    四、代码工程及地址

    核心代码红色方框所示

    https://gitee.com/jike11231/redisDemo.git

    图片

    https://mp.weixin.qq.com/s/VBr3E086U58PyQkNdFfNzg

  • 相关阅读:
    uniapp开发H5微信公众号,使用微信的扫一扫等Api试错
    125. 验证回文串
    【Python】ModuleNotFoundError: No module named ‘Crypto‘
    Spring 统一功能处理(拦截器)
    redis启动和简单使用
    【MEIF:ℓ1-ℓ0混合分解】
    每章一篇博客带你拿下吉林大学JAVAEE期末(八)
    JS实时获取录音数据并播放
    上海IB国际学校介绍
    【JavaWeb - 网页编程】一 HTML
  • 原文地址:https://blog.csdn.net/weixin_38933806/article/details/133997648