在Redis中获得几个哈希算法的最有效的方式? [英] Most efficient way to get several hashes in Redis?

查看:259
本文介绍了在Redis中获得几个哈希算法的最有效的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经阅读过这篇文章作为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中获得几个哈希算法的最有效的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆