如何在 redis 键命令中应用“或"条件 [英] How to apply 'OR' condition in redis keys command
本文介绍了如何在 redis 键命令中应用“或"条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如
keys X 'or' Y
应该打印
1)X
2)Y
推荐答案
不,你不能直接用 KEYS
或 SCAN
来做,因为模式匹配他们提供的不支持.
No, you can't do it directly with KEYS
nor with SCAN
, as the pattern matching that they offer does not support that.
但是,您可以使用 Lua 脚本来执行此操作:
You can, however, use a Lua script to do that:
local dict = {}
while (#ARGV > 0) do
local pattern = table.remove(ARGV,1)
local cursor = 0
repeat
local r = redis.call('SCAN',cursor,'MATCH',pattern)
cursor = tonumber(r[1])
while (#r[2] > 0) do
dict[table.remove(r[2],1)] = true
end
until cursor == 0
end
local reply = {}
for k,v in pairs(dict) do
reply[#reply+1] = k
dict[k] = nil
end
return reply
示例输出:
$ redis-cli KEYS "*"
1) "bazfoo"
2) "bar2"
3) "foo1"
4) "qazbar"
$ redis-cli --eval scanor.lua , "foo*" "bar*"
1) "bar2"
2) "foo1"
这篇关于如何在 redis 键命令中应用“或"条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文