Is there MGET analog for Redis hashes?

前端 未结 5 1914
庸人自扰
庸人自扰 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:33

    There is no command to do it on one shot, but there is a way to do it "nicely", using a list (or sorted set) where you would store you hashKeys, and then retrieve them as bulk using multi.

    In PHP:

    $redis->zAdd("myHashzSet", 1, "myHashKey:1");
    $redis->zAdd("myHashzSet", 2, "myHashKey:2");
    $redis->zAdd("myHashzSet", 3, "myHashKey:3");
    
    $members = $redis->zRange("myHashzSet", 0, -1);
    $redis->multi();
    foreach($members as $hashKey) {
        $redis->hGetAll($hashKey);
    }
    $results = $redis->exec();
    

    I recommand using a sorted set, where you use the score as an ID for your hash, it allows to take advantages of all score based command.

提交回复
热议问题