我希望Redis的所有密钥都不包含特定字符,即':'
示例:
Keys in redis: ab, cd, a:b, c:d
Query: keys ???
Expected result: ab, cd
答案 0 :(得分:2)
KEYS
命令无法实现这一点,因为它只支持以下的glob样式模式:
h?llo
匹配hello
,hallo
和hxllo
h*llo
匹配hllo
和heeeello
h[ae]llo
匹配hello
和hallo
,但不匹配hillo
这是一个Lua script,您可以使用它来查找不包含冒号的所有键(:
):
local keys = {}
local cursor = "0"
repeat
local ret = redis.call("scan", cursor)
cursor = ret[1]
for _, key in ipairs(ret[2]) do
if not string.find(key, ":", 1, true) then
keys[#keys + 1] = key
end
end
until cursor == "0"
return keys