Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there MGET analog for Redis hashes?

Tags:

redis

I'm planning to start using hashes insead of regular keys. But I can't find any information about multi get for hash-keys in Redis wiki. Is this kind of command is supported by Redis?

Thank you.

like image 819
Kirzilla Avatar asked Jul 25 '10 13:07

Kirzilla


People also ask

How does Redis hash work?

Redis is an open-source, in-memory key-value data store. A Redis hash is a data type that represents a mapping between a string field and a string value. Hashes can hold many field-value pairs and are designed to not take up much space, making them ideal for representing data objects.

Does Redis use hashing?

Redis hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things.

Which of the following does Redis hashes stores?

Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects. In Redis, every hash can store up to more than 4 billion field-value pairs.

Why use Redis hash?

Redis hashes are also ideal for representing objects. For example, a User object can be represented with a Redis hash, where each key-value pair contains a different attribute (e.g. username, email address, age, etc.). Redis can store hashes with less than 100 key-value pairs in a very space-efficient manner.


2 Answers

You can query hashes or any keys in pipeline, i.e. in one request to your redis instance. Actual implementation depends on your client, but with redis-py it'd look like this:

pipe = conn.pipeline() pipe.hgetall('foo') pipe.hgetall('bar') pipe.hgetall('zar') hash1, hash2, hash3 = pipe.execute() 

Client will issue one request with 3 commands. This is the same technique that is used to add multiple values to a set at once.

Read more at http://redis.io/topics/pipelining

like image 144
kmerenkov Avatar answered Sep 19 '22 19:09

kmerenkov


If SORT let you use multiple GETs with the -> syntax, and all your hashes had the same fields, you could get them in a bulk reply by putting their names into a set and sorting that.

  SORT names_of_hashes GET *->field1 *->field2 *->field3 *->etc 

But it doesn't look like you can do that with the hash access. Plus you'd have to turn the return list back into hashes yourself.

UPDATE: Redis seems to let you fetch multiple fields if you name your hashes nicely:

redis> hset hash:1 name fish (integer) 1 redis> hset hash:2 name donkey (integer) 1 redis> hset hash:3 name horse (integer) 1 redis> hset hash:1 type fish (integer) 1 redis> hset hash:2 type mammal (integer) 1 redis> hset hash:3 type mammal (integer) 1 redis> sadd animals 1 (integer) 1 redis> sadd animals 2 (integer) 1 redis> sadd animals 3 (integer) 1 redis> sort animals get # get hash:*->name get hash:*->type 1. "1" 2. "fish" 3. "fish" 4. "2" 5. "donkey" 6. "mammal" 7. "3" 8. "horse" 9. "mammal" 
like image 34
rjp Avatar answered Sep 19 '22 19:09

rjp