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库存数据
    • 调用效果
相关推荐
lang201509281 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
007php0071 小时前
百度面试题解析:微服务架构、Dubbo、Redis及其一致性问题(一)
redis·百度·docker·微服务·容器·职场和发展·架构
长安城没有风2 小时前
从入门到精通【Redis】Redis 典型应⽤ --- 分布式锁
数据库·redis·分布式
Q_Q5110082855 小时前
python+uniapp基于微信小程序团购系统
spring boot·python·微信小程序·django·uni-app·node.js·php
爬山算法6 小时前
Redis(69)Redis分布式锁的优点和缺点是什么?
数据库·redis·分布式
重生之我在二本学院拿offer当牌打6 小时前
手写SpringBoot Starter(一):10分钟带你入门,从此告别重复配置!
spring boot
初见0016 小时前
🌱 SpringBoot自动配置:别装了,我知道你的秘密!🤫
spring boot·后端
lang201509286 小时前
Spring Boot核心功能深度解析
spring boot
2401_837088507 小时前
Redis通用命令
数据库·redis·缓存
嵌入式-老费7 小时前
Easyx图形库应用(用lua开发图形界面)
开发语言·lua