reds使用lua脚本最后的set命令失败

新手上路,请多包涵

主要问题

我想使用lua来做一个redis库存的管理,最后set一直失败,最后发现scan的循环处理结构影响了它,但是我对lua了解的不多,不知道是什么原因

相关代码

local ans, has, cursor = {}, {}, "0";
local amount = KEYS[1]
local order_values
local order_amount = 0
local redis_amount = 0
local db_amount = ARGV[2]
local quality = ARGV[3]
if redis.call("get",amount) == false then

--去掉循环后成功
repeat
    local t = redis.call("SCAN", cursor, "MATCH", ARGV[1], "COUNT", 100);
    local list = t[2];
    for i = 1, #list do
        local s = list[i];
        if has[s] == nil then has[s] = 1; ans[#ans + 1] = s; end;
    end;
    cursor = t[1];
until cursor == "0";

if next(ans) == nil then
    order_amount = 0;
else  
    order_values = redis.call("mget", unpack(ans));
    for i, k in ipairs(order_values) do
        order_amount = order_amount + order_values[i]
    end;
end;
redis_amount = db_amount - order_amount;
if redis_amount < 0 then
    return 2;
end; 
--返回false
return redis.call("set", KEYS[1], redis_amount)

end;

return redis.call("set", KEYS[1], redis_amount)

直接set也是可以的 只要运行了repeat就会false

阅读 1.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题