Most efficient way to get several hashes in Redis?

2019-02-08 00:02发布

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.

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!

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-08 00:56

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

More information about pipelines can be found here: http://redis.io/topics/pipelining

查看更多
登录 后发表回答