如何在Redis中迭代对象并获取所有值?像这样:
scan object* hgetall
这应该返回
age 22 password 1234 username Sarah
age 20 password 5678 username Mike
当Redis包含此数据时:
hmset objectSarah age 22 password 1234 username Sarah
hmset objectMike age 20 password 5678 username Mike
由于
答案 0 :(得分:1)
只用一个命令就不能这样做,你需要逐个扫描然后处理结果(即HGETALL
)。
EVAL
)中,该脚本接受当前游标并在将所有内容返回给客户端之前获取数据。即:
local cur, pat = tonumber(ARGV[1]), ARGV[2]
local scan = redis.call('SCAN', cur, 'MATCH', pat)
for i, v in ipairs(scan[2]) do
local data = redis.call('HGETALL', v)
scan[2][i] = { v, data }
end
return scan
运行时示例:
127.0.0.1:6379> hmset objectSarah age 22 password 1234 username Sarah
OK
127.0.0.1:6379> hmset objectMike age 20 password 5678 username Mike
OK
127.0.0.1:6379> script load "..."
"d81ace3345591b4fee7f3bf4cf28e7b260b59554"
127.0.0.1:6379> evalsha d81ace3345591b4fee7f3bf4cf28e7b260b59554 0 0 object*
1) "0"
2) 1) 1) "objectSarah"
2) 1) "age"
2) "22"
3) "password"
4) "1234"
5) "username"
6) "Sarah"
2) 1) "objectMike"
2) 1) "age"
2) "20"
3) "password"
4) "5678"
5) "username"
6) "Mike"
注意:
HGETALL
w / big hashes。