如何在 Redis 中对哈希的集合和键进行 SINTER?
How to SINTER a Set and keys of a Hash in Redis?
假设我有一个集合和一个哈希:
SADD a 1 2 3
HSET b 1 "value1" 2 "value2"
现在我想得到 Set a
和 Hash b
的键之间的交集。可以用 Redis 做吗?
以下无效:
SINTER a b # (error) WRONGTYPE Operation against a key holding the wrong kind of value
目前只能用Lua脚本实现:
eval 'local res = {}; for i, v in ipairs(redis.call("smembers", KEYS[1])) do if redis.call("hexists", KEYS[2], v) > 0 then res[#res + 1] = v end end return res' 2 set-key hash-key
一个更紧凑的解决方案是使用SORT
命令。但是,到目前为止,SORT
命令不支持“*”作为字段名称的 glob 样式 GET 模式。我已经为它创建了一个 feature request。
假设我有一个集合和一个哈希:
SADD a 1 2 3
HSET b 1 "value1" 2 "value2"
现在我想得到 Set a
和 Hash b
的键之间的交集。可以用 Redis 做吗?
以下无效:
SINTER a b # (error) WRONGTYPE Operation against a key holding the wrong kind of value
目前只能用Lua脚本实现:
eval 'local res = {}; for i, v in ipairs(redis.call("smembers", KEYS[1])) do if redis.call("hexists", KEYS[2], v) > 0 then res[#res + 1] = v end end return res' 2 set-key hash-key
一个更紧凑的解决方案是使用SORT
命令。但是,到目前为止,SORT
命令不支持“*”作为字段名称的 glob 样式 GET 模式。我已经为它创建了一个 feature request。