Created
November 1, 2021 08:30
-
-
Save kougazhang/45e12f566e60088491657f7b5989479d to your computer and use it in GitHub Desktop.
#lua #redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
删除 redis 内 state