Is there MGET analog for Redis hashes?

前端 未结 5 1913
庸人自扰
庸人自扰 2020-12-13 08:56

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 support

5条回答
  •  别那么骄傲
    2020-12-13 09:23

    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"
    

提交回复
热议问题