Skip to content

Instantly share code, notes, and snippets.

@kougazhang
Created November 1, 2021 08:30
Show Gist options
  • Save kougazhang/45e12f566e60088491657f7b5989479d to your computer and use it in GitHub Desktop.
Save kougazhang/45e12f566e60088491657f7b5989479d to your computer and use it in GitHub Desktop.
#lua #redis
-- scan whole keys in redis by key_pattern and filter
-- key_pattern: key regexp
-- count: match count for once scan
-- filter: one function that accepts the value of key as param.
local scan=function(key_pattern, count, filter)
local start = "-1"
local lst = {}
while (start ~= "0") do
if (start == "-1") then
start = "0"
end
local res = redis.call("scan", start, "match", key_pattern, "count", count)
start = res[1]
local keys = res[2]
if (filter ~= nil) then
for i, key in ipairs(keys) do
local value = redis.call("get", key)
if (filter(value)) then
table.insert(lst, key)
end
end
end
end
return lst
end
-- filter example
local filter=function(val)
return tonumber(val) > 0
end
-- example: scan all keys in redis that key pattern matchs "sns-*com*" and the value is bigger than 0.
return scan("sns-*com*", 100, filter)
@kougazhang
Copy link
Author

删除 redis 内 state

EVAL "for i=0,8 do redis.call('del', 'filex:state:fbaidu-upmov-y-'..i) end" 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment