[Add] CacheStore-Redis 添加两个 Lua 操作脚本;

[Add] CheckElementContains.lua 添加用于检查 List 中是否含有某元素的脚本;
[Add] RemoveElementByIndex.lua 添加用于通过指定索引删除元素的脚本;
This commit is contained in:
LamGC 2021-01-02 22:50:34 +08:00
parent b9141f9f96
commit 72ff066f5c
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,25 @@
local key = tostring(KEYS[1])
local value = ARGV[1]
local step = 175
if (key == nil) or (value == nil) then
return -1
end
if not (ARGV[2] == nil) then
step = tonumber(ARGV[2])
end
-- 步进循环搜索, 找到了立刻返回
local listLength = redis.call('llen', key);
for i = 0, listLength, step do
local storeValue = redis.call("lrange", key, i, i + step)
for listIndex, v in ipairs(storeValue) do
if value == v then
return i + listIndex - 1;
end
end
end
-- 如果没找到索引, 返回 0
return -1

View File

@ -0,0 +1,28 @@
local key = tostring(KEYS[1])
local index = tonumber(ARGV[1])
if (key == nil) or (index == nil) then
return -1
end
local function getRandom(n)
local t = {
"0","1","2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
}
local s = ""
for _ = 1, n do
s = s .. t[math.random(#t)]
end;
return s
end;
local flag = getRandom(24)
if (redis.call("llen", key) <= index) or redis.call("lIndex", key, index) == nil then
return -1
else
redis.call("lSet", key, index, flag);
return redis.call("lRem", key, 0, flag);
end