我有一个结构
data_type:key1 - hash
data_type:key2 - hash
data_type:key3 - hash
data_type:key4 - hash
data_type:key5 - hash
data_type:index - set(key1, key2, key3, key4, key5)
是否可以使用 redis 中的 lua 构建一个脚本,该脚本将遍历集合 data_type:index 并将所有 data_type:key* 作为哈希列表返回?我仍在学习 Lua,所以在我的脑海中,我认为它会像这样工作
collect = []
for key_name in redis.call.smemembers('data_type:index'):
collect.append( redis.call.smembers('data_type:' + key_name)
return collect
一般大多数索引都有大约 100 个键,每个键大约 1KB,因此这个脚本在理想情况下会有 100-120KB 的响应大小。
在有人问之前,真正的键看起来像'some_data:status:{64 bit hex string}'和'some_data:index:2013:05:09',{64 bit hex string}是:索引集。
最佳答案
检查 SSCAN command .
类似下面的内容应该适用于您的情况:
local collect = {}
local match_pattern = "*"
local results = redis.call("SSCAN", "data_type:index", 0, "match", match_pattern)
for i, key_name in ipairs(results[2]) do
-- your code here (could be different depending on your needs)
local key_value = redis.call("GET", "data_type:" .. key_name)
if key_value then
table.insert(collect, key_value)
end
end
return collect
关于performance - 可能在 redis 中使用 lua 将存储在集合中的所有 key 作为哈希列表返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16465354/