'keys'命令的Redis模式返回除包含特定char的键之外的所有键

时间:2014-11-14 10:58:19

标签: redis

我希望Redis的所有密钥都不包含特定字符,即':'

示例:

Keys in redis: ab, cd, a:b, c:d
Query: keys ???
Expected result: ab, cd

1 个答案:

答案 0 :(得分:2)

KEYS命令无法实现这一点,因为它只支持以下的glob样式模式:

  
      
  • h?llo匹配hellohallohxllo
  •   
  • h*llo匹配hlloheeeello
  •   
  • h[ae]llo匹配hellohallo,但不匹配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