内容简介:翻译自:https://stackoverflow.com/questions/29942541/how-to-get-keys-which-does-not-match-a-particular-pattern-in-redis
重要提示:始终使用SCAN而不是(邪恶的)KEYS
Redis的模式匹配在某种程度上受到功能限制(参见 util.c 中stringmatchlen的实现),并没有提供您寻求ATM的功能.也就是说,考虑以下可能的路线:
>扩展stringmatchlen以符合您的要求,可能将其作为PR提交.
>考虑一下你要做的事情 – 除非你为它们编制索引,否则获取一个密钥子集总是效率低下,考虑跟踪所有非用户密钥的名称(即在Redis集中).
>如果你真的坚持扫描整个键空间并匹配负面模式,那么实现这一点的一种方法就是使用一点点 Lua 魔法.
请考虑以下数据集和脚本:
127.0.0.1:6379> dbsize (integer) 0 127.0.0.1:6379> set user:1 1 OK 127.0.0.1:6379> set use:the:force luke OK 127.0.0.1:6379> set non:user a OK
Lua(将此保存为scanregex.lua):
local re = ARGV[1]
local nt = ARGV[2]
local cur = 0
local rep = {}
local tmp
if not re then
re = ".*"
end
repeat
tmp = redis.call("SCAN", cur, "MATCH", "*")
cur = tonumber(tmp[1])
if tmp[2] then
for k, v in pairs(tmp[2]) do
local fi = v:find(re)
if (fi and not nt) or (not fi and nt) then
rep[#rep+1] = v
end
end
end
until cur == 0
return rep
输出 – 第一次常规匹配,第二次补码:
foo@bar:~$redis-cli --eval scanregex.lua , "^user" 1) "user:1" foo@bar:~$redis-cli --eval scanregex.lua , "^user" 1 1) "use:the:force" 2) "non:user"
翻译自:https://stackoverflow.com/questions/29942541/how-to-get-keys-which-does-not-match-a-particular-pattern-in-redis
以上所述就是小编给大家介绍的《如何获得与redis中特定模式不匹配的键?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- scala.util.Try:如何获得Throwable值?模式匹配?
- 给我一首歌的时间,带你深入理解正则表达式的贪婪匹配和懒惰匹配
- 2分钟获得HTTPS证书
- Leetcode 题目:括号匹配
- Nginx 请求匹配规则
- WAF分组安全策略匹配
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Build Your Own Web Site the Right Way Using HTML & CSS
Ian Lloyd / SitePoint / 2006-05-02 / USD 29.95
Build Your Own Website The Right Way Using HTML & CSS teaches web development from scratch, without assuming any previous knowledge of HTML, CSS or web development techniques. This book introduces you......一起来看看 《Build Your Own Web Site the Right Way Using HTML & CSS》 这本书的介绍吧!