springboot lua检查redis库存

需求

最近需求需要实现检查多个马戏场次下的座位等席对应库存渠道的库存余量,考虑到性能,决定采用Lua脚本实现库存检查。

数据结构

  • 库存层级结构
  • redis库存hash类型结构

实现

  • lua脚本
lua 复制代码
--- 字符串分割为数组
local function split(str, char)
    local arr = {};
    local pattern = string.format('([^%s]+)', char);
    for item in string.gmatch(str, pattern) do
        table.insert(arr, item);
    end
    return arr;
end
--- 数组转成map
local function toMap(tb)
    if (tb == nil) then
        return {};
    end
    local map = {};
    for i = 1, #tb do
        if (i % 2 == 1) then
            map[tb[i]] = tb[i+1];
        end
    end
    return map;
end
--- 初始化数据,入参01:1,02:2.01:3
local argArr = {};
for rowItem in string.gmatch(ARGV[1], '([^.]+)') do
    local row = {};
    for keyVal in string.gmatch(rowItem, '([^,]+)') do
    -- 解析出每个key与value
        local tempArr = split(keyVal, ':');
        row[tempArr[1]] = tempArr[2];
    end
    table.insert(argArr, row);
end
--- 检查库存
for i = 1, #KEYS do
    --local rData = redis.call("HMGET", KEYS[i], '01 02');
    local rdsMap = toMap(redis.call("HGETALL", KEYS[i]));
    for tier, quantity in pairs(argArr[i]) do
        --redis.log(redis.LOG_NOTICE, string.format('key is:%s,tier is:%s,quantity is:%s', KEYS[i], tier, quantity));
        if (rdsMap[tier] == nil or tonumber(quantity) > tonumber(rdsMap[tier])) then
            return string.format("库存key:%s,tier:%s不足", KEYS[i], tier);
        end
    end
end
return '';

遍历键值对时需要使用pairs而不是ipairs,二者区别如下:

for k,v in pairs(argArr)的遍历顺序并非是table类型数据的排列顺序,而是根据table中key的hash值排列的顺序来遍历的。

for k,v in ipairs(argArr)必须要求table中的key为有序的,而且必须是从1开始,ipairs只会从1开始按连续的key顺序遍历到key不连续为止。

  • 入参设计
    由于RedisTemplate直接传入集合为参数序列化后会有中括号,而lua脚本table类型使用花括号定义,因此采用字符串作为入参,在lua脚本中解析字符串为table类型。例如参数01:1,02:2.01:3代表二维数组,第一行为01:1,02:2,其中01为渠道,1为渠道对应的库存,英文逗号作为渠道分隔符。
  • springboot调用lua脚本
java 复制代码
@Autowired
private StringRedisTemplate redisJsonTemplate;

@Test
public void checkInventory() {
    List<String> keyList = List.of("stock:GZ7:433680411156197407", "stock:GZ7:433680411156197408");
    // 执行 lua 脚本
    DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();
    // 指定 lua 脚本
    redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("/lua/TheaterStockCheck.lua")));
    // 指定返回类型
    redisScript.setResultType(String.class);
    Object result = redisJsonTemplate.execute(redisScript, keyList, "01:1,0101:2.01:3");
    //01:1,02:2.01:3
    System.out.println(result);
}

调用lua脚本需要使用string序列化方式,使用jdk序列化方式会导致序列化后的数据lua脚本无法解析

  • 测试效果
    • redis库存数据
    • 调用效果
相关推荐
光依旧7 分钟前
MCP实战手记系列(十):MCP Server高可用实战
spring boot·高可用·nginx负载均衡·redis哨兵·mcp·微服务容错·resilience4j熔断
禾小西2 小时前
Redis:从两大维度和三大主线建立知识体系
数据库·redis·缓存
禾小西3 小时前
Redis 数据结构:快速的 Redis 有哪些慢操作?
数据结构·数据库·redis
省钱兄--zs3 小时前
西安24小时自助健身房解决方案实战:系统开发与部署全流程指南
java·spring boot·系统架构·intellij-idea·需求分析
ly76895 小时前
Spring Boot 集成 Elasticsearch 的生产实践:客户端选型、索引生命周期与批量写入容错
spring boot·elasticsearch·索引生命周期·java api client·批量写入
程序猿_极客6 小时前
【免费】分享一套优质的基于SpringBoot的服装商城管理系统的设计与实现(带可视化图表、协同过滤功能),源码+文档+视频详解(讲解)
java·spring boot·后端·服装商城管理系统
bug菌6 小时前
🤔同事突然问我:Spring的注解 @Component 和 @Service 有何不同?
java·spring boot·后端
行百里er7 小时前
Redis 事务与 Pipeline:批量操作的正确姿势
redis
Lonely丶墨轩7 小时前
用 Vue + Spring Boot + Python 做一个 AI 双人海龟汤游戏:从出题、联机到自动审稿等核心技术设计与实现
spring boot·后端·游戏
fengkai45458 小时前
十二、Redis -1
运维·数据库·redis