当前位置: 首页 > news >正文

springboot整合lua脚本在Redis实现商品库存扣减

1、目的

        使用lua脚本,可以保证多条命令的操作原子性;同时可以减少操作IO(比如说判断redis对应数据是否小于0,小于0就重置为100,这个场景一般是取出来再判断,再存放进行,就至少存在2次IO,用lua脚本一条命令1次IO就解决了,在批量扣减情况存在多次IO,lua脚本1次也可以解决),提高速度,降低IO.

2、使用案列

        根据传入的产品标识及数量扣减该产品数量;此处为单个产品扣减,可优化为批量产品传入,lua内部用table处理。

2.1 初始化redis参数

添加产品及库存(hash结构)。添加两种水果的库存。

// 向Redis中添加数据
redisTemplate.opsForHash().put("productMap", "pro1", "{\"name\":\"苹果\",\"stock\":100}");
redisTemplate.opsForHash().put("productMap", "pro2", "{\"name\":\"西瓜\",\"stock\":1200}");

查看结果:

2.2 业务代码

传入lua脚本,实现对应产品库存数量扣减。(可优化为多产品批量扣减)

通过setnx加锁,防止死锁设置锁超时时间,同时业务执行完手动释放锁。设置锁等待时间、及锁等待轮询获取锁。(eg:自旋释放cpu资源重新抢占资源)

@Test
public void tete(){// 向Redis中添加数据redisTemplate.opsForHash().put("productMap", "pro1", "{\"name\":\"苹果\",\"stock\":100}");redisTemplate.opsForHash().put("productMap", "pro2", "{\"name\":\"西瓜\",\"stock\":1200}");// Lua 脚本字符串    String luaScript = "local productKey = KEYS[1]; " +"local pro = KEYS[2]; " +"local lockKey = KEYS[3]; " +"local lockTimeout = tonumber(ARGV[1]); " +"local deductAmount = tonumber(ARGV[2]); " +"local spinIntervalMs = tonumber(ARGV[3]); " +"local maxSpinCount = tonumber(ARGV[4]); " +"local lockAcquired = redis.call('setnx', lockKey, 1); " +"if lockAcquired == 1 then " +"    redis.call('pexpire', lockKey, lockTimeout); " +"    local currentValue = redis.call('hget', productKey, pro); " +"    if currentValue then " +"        local dbObj = cjson.decode(currentValue);" +"        local currentStock = tonumber(dbObj.stock); " +"        if currentStock >= deductAmount then " +"            dbObj.stock = currentStock - deductAmount; " +"            local updatedValue = cjson.encode(dbObj); " +"            redis.call('hset', productKey, pro, updatedValue); " +"            redis.call('del', lockKey); " +  // 释放锁"            return true; " +"        else " +"            return false; " +"        end " +"    else " +"        return false; " +"    end " +"else " +"    local spinCount = 0; " +"    while spinCount < maxSpinCount do " +"        local lockValue = redis.call('get', lockKey); " +"        if not lockValue then " +"            lockAcquired = redis.call('setnx', lockKey, 1); " +"            if lockAcquired == 1 then " +"                redis.call('pexpire', lockKey, lockTimeout); " +"                local currentValue = redis.call('hget', productKey, pro); " +"                if currentValue then " +"                   local dbObj = cjson.decode(currentValue);" +"                   local currentStock = tonumber(dbObj.stock); " +"                    if currentStock >= deductAmount then " +"                        dbObj.stock = currentStock - deductAmount; " +"                        local updatedValue = cjson.encode(dbObj); " +"                        redis.call('hset', productKey, pro, updatedValue); " +"                        redis.call('del', lockKey); " +  "                        return true; " +"                    else " +"                        return false; " +"                    end " +"                else " +"                    return false; " +"                end " +"            end " +"            break; " +"        end " +"        spinCount = spinCount + 1; " +"    end " +"    return false; " +"end";// 创建DefaultRedisScript对象DefaultRedisScript<Boolean> script = new DefaultRedisScript<>();script.setScriptText(luaScript);script.setResultType(Boolean.class); // 设置返回类型为Boolean// 执行脚本Boolean result = (Boolean) redisTemplate.execute(script,Collections.unmodifiableList(List.of("productMap","pro1","lock:pro1")), // KEYS参数"50000", // ARGV参数第一个:锁过期时间(毫秒)"10", // ARGV参数第二个:扣减数量"1000",// ARGV参数第3个:等待时间"5");// ARGV参数第4个:轮询次数System.out.println("result1:"+result);Boolean result2 = (Boolean) redisTemplate.execute(script,Collections.unmodifiableList(List.of("productMap","pro1","lock:pro1")), // KEYS参数"50000", // ARGV参数第一个:锁过期时间(毫秒)"10", // ARGV参数第二个:扣减数量"1000","5");System.out.println("result2:"+result2);Boolean result3 = (Boolean) redisTemplate.execute(script,Collections.unmodifiableList(List.of("productMap","pro1","lock:pro1")), // KEYS参数"50000", // ARGV参数第一个:锁过期时间(毫秒)"10", // ARGV参数第二个:扣减数量"1000","5");System.out.println("result3:"+result3);if (result2) {System.out.println("Stock deduction successful.");} else {System.out.println("Insufficient stock or lock already acquired.");}// 验证库存是否正确扣减Object updatedValue = redisTemplate.opsForHash().get("productMap", "pro1");System.out.println(updatedValue);Boolean result5 = (Boolean) redisTemplate.execute(script,Collections.unmodifiableList(List.of("productMap","pro2","lock:pro2")), // KEYS参数"5000", // ARGV参数第一个:锁过期时间(毫秒)"500", // ARGV参数第二个:扣减数量"1000","5");
}

2.3 正常执行结果

2.4 若获取锁超时,则会出现扣减失败

脚本执行时间过长会导致。(此处可通过删除手动释放锁实现:模拟业务耗时过长没办法手动释放锁需等待锁国企时间)

第二个扣减等待超时。可通过设置调整添加自旋时间重试或业务代码判断重试机制

http://www.lryc.cn/news/502581.html

相关文章:

  • MySQL ON DUPLICATE KEY UPDATE影响行数
  • uniapp小程序 slot中无法传递外部参数的解决方案
  • umi实现动态获取菜单权限
  • Pytest-Bdd-Playwright 系列教程(14):Docstring 参数
  • 交互开发---测量工具(适用VTK或OpenGL开发的应用程序)
  • Qt 一个简单的QChart 绘图
  • 【Java笔记】LinkedList 底层结构
  • el-table组件树形数据修改展开箭头
  • 太速科技-FMC154-基于FMC 八路SFP+万兆光纤子卡
  • 记:排查设备web时慢时快问题,速度提升100%
  • 音视频入门基础:MPEG2-TS专题(13)——FFmpeg源码中,解析Section Header的实现
  • 根据PDF模板单个PDF导出到浏览器和多个PDF打包ZIP导出到浏览器
  • 如何创建一个基本的Spring Boot应用程序
  • 1.2 计算机网络的分类和应用(重要知识点)
  • @JsonSerialize失效解决
  • Docker部署WebRTC-Streamer
  • 2025年的大模型计划重点在于跨领域智能、工作流自动化、多模态能力强化
  • day12 接口测试 ——入门→精通→实战(1)
  • 伏羲0.07(文生图)
  • scala的泛型特质的应用场景
  • Win10环境vscode+latex+中文快速配置
  • 【vue2】el-select,虚拟滚动(vue-virtual-scroller)
  • 【ETCD】[源码阅读]深度解析 EtcdServer 的 processInternalRaftRequestOnce 方法
  • 【RabbitMQ】RabbitMQ中核心概念交换机(Exchange)、队列(Queue)和路由键(Routing Key)等详细介绍
  • 【AI知识】过拟合、欠拟合和正则化
  • 计算机毕设-基于springboot的航空散货调度系统的设计与实现(附源码+lw+ppt+开题报告)
  • 视图、转发与重定向、静态资源处理
  • 优选算法——分治(快排)
  • 【Linux系统】文件系统
  • javaweb的基础