commit message

This commit is contained in:
Chopper
2021-05-13 10:41:46 +08:00
commit 3785bdb3bb
1424 changed files with 100110 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
local c
c = redis.call('get',KEYS[1])
-- 调用不超过最大值,则直接返回
if c and tonumber(c) > tonumber(ARGV[1]) then
return c;
end
-- 执行计算器自加
c = redis.call('incr',KEYS[1])
if tonumber(c) == 1 then
-- 从第一次调用开始限流,设置对应键值的过期
redis.call('expire',KEYS[1],ARGV[2])
end
return c;

View File

@@ -0,0 +1,62 @@
-- 可能回滚的列表一个记录要回滚的skuid一个记录库存
local id_list= {}
local quantity_list= {}
-- 调用放传递的keys 和 values execute(RedisScript<T> script, List<K> keys, Object... args)
local keys = KEYS
local values = ARGV;
local function deduction(key,num)
keys[1] = key;
local value = redis.call("get",keys[1])
if not value then
value = 0;
end
value = value + num
-- 变更后库存数量小于
if(value<0)
then
-- 发生超卖
return false;
end
redis.call("set",keys[1],value)
return true
end
local function rollback()
for i,k in ipairs (id_list) do
-- 还原库存
keys[1] = k;
redis.call("incrby",keys[1],0-quantity_list[i])
end
end
local function execute()
-- i 类java for循环 for(int i=0;i<?;i++) 下标
-- k 为遍历的值 具体值,非下标
for i, k in ipairs (values)
do
-- num 变更数量
-- key 为缓存key
local num = tonumber(k)
local key= keys[i]
-- 进行库存扣减为false 代表扣减失败,要进行回滚
local result = deduction(key,num)
-- 回滚
if (result == false)
then
rollback()
return false
else
-- 记录可能要回滚的数据
table.insert(id_list,key)
table.insert(quantity_list,num)
end
end
return true;
end
return execute()