问题描述
所以我已经阅读了这篇文章关于那里没有作为 Redis 哈希的 MGET
模拟.答案之一说使用 MULTI/EXEC
进行批量操作,这对列表和常规键有效,但对哈希无效.然而,现在,我正在为我想要检索的每个散列进行网络调用,这对我来说似乎是个坏消息.
So I've already read this post about there not being an MGET
analog for Redis hashes. One of the answers said to use MULTI/EXEC
to do the operation in bulk, and that does work for lists and regular keys, but not for hashes, unfortunately. Right now, however, I'm doing a call over the wire for every single hash I want to retrieve which seems like bad news to me.
所以我的问题是:从 Redis 取回多个哈希值的最方法是什么,效率标准是最少的网络调用次数?我正在使用 Redis 2.0.4,使用 Python 客户端进行编程.谢谢!
So my question is: what is the most efficient way to get several hashes back from Redis, with the standard of efficiency being the least number of network calls? I'm using Redis 2.0.4, programming with the Python client. Thanks!
推荐答案
最有效的方法是使用管道.
The most efficient way would be using a pipeline.
假设您想要给定密钥的所有内容并且已经知道所有密钥:
Assuming you want everything for a given key and know all the keys already:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
p = r.pipeline()
for key in keys:
p.hgetall(key)
for h in p.execute():
print h
可以在此处找到有关管道的更多信息:http://redis.io/topics/pipelining
More information about pipelines can be found here: http://redis.io/topics/pipelining
这篇关于在 Redis 中获取多个散列的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!