redis_lua.removeById2.lua Maven / Gradle / Ivy
The newest version!
local table = KEYS[1];
local primaryKey = KEYS[2];
local indexHashes = KEYS[3];
function string.split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
-- 删除索引
local field = table .. ":" .. primaryKey
local indexVal = redis.call('hget', indexHashes, field);
if indexVal then
local indexValArray = string.split(indexVal, ',');
for i, v in ipairs(indexValArray) do
local splitArray = string.split(v, ':');
if splitArray[2] == 'index' then
-- 聚集索引
local exists = redis.call("exists", table .. ":index:" .. splitArray[1] .. ":" .. splitArray[3]);
if exists == 1 then
redis.call('srem', table .. ":index:" .. splitArray[1] .. ":" .. splitArray[3], primaryKey);
end
elseif splitArray[2] == 'unique' then
-- 唯一索引
--local exists = redis.call("hexists", table .. ":unique:", splitArray[1] .. ":" .. splitArray[3])
--if exists == 1 then
redis.call('hdel', table .. ":unique:", splitArray[1] .. ":" .. splitArray[3]);
--end
end
end
end
-- 删除索引值
redis.call('hdel', indexHashes, field);
-- 删除对象值
redis.call('hdel', table, primaryKey);
© 2015 - 2024 Weber Informatics LLC | Privacy Policy