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库存数据
    • 调用效果
相关推荐
阿杆6 小时前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
AAA修煤气灶刘哥17 小时前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
昵称为空C20 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
麦兜*1 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*1 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu1 天前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
计算机毕业设计木哥1 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
凯子坚持 c1 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
weixin_456904271 天前
跨域(CORS)和缓存中间件(Redis)深度解析
redis·缓存·中间件