Reputation: 32130
Is there a way to have the following in a 1-request 1-response way?
given a redis set with key name and for each key has a redis hash with fields
How can I fetch the hashes of those keys in one go?
something like this, which is the best I can do (not sure this will work, but you get the idea)
hashes = []
keys = redis.smembers("myset")
redis.multi do
keys.map do |k|
hashes << redis.hgetall(k)
end
end
hashes = hash.map(&:value) # to resolve future values
but this does at least two requests (which isn't the best but ok), not sure how Redis::Future resolves to value (if it sends another request or not)
Upvotes: 1
Views: 697
Reputation: 5836
No, Redis don't have a command which would return hashes for multiple keys* at once. You're stuck with n + 1 requests - one to get the keys, and one for each key.
You could try to write a Lua script, which to do all that in Redis and return the combined results. See the documentation of the EVAL command for an introduction to scripting in Redis. Note that if you do this, you cannot use a cluster, as keys must be explicitly provided to the script for it to be safe in a cluster.
*MGET does return values for multiple keys, but only if they're strings.
Upvotes: 2