0

I am using phpredis in my application and I have the following data structure. The account Id acts as the key for each user:

    $data = array(
        "accId1"=> array("userId" => "user0234", "username" => "apples", "appversion" => "1.0"),
        "accId2"=> array("userId" => "user2342", "username" => "roses", "appversion" => "2.0")
....
);

To store the above in the Redis I use pipelines like so:

$pipeline = $redis->multi(Redis::PIPELINE);
        foreach ($data as $accId => $userInfo) {
            $pipeline->hMSet($accId, $userInfo);
        }
        $pipeline->exec();

For retrieval:

$accIdSet = getAccountIds();
$pipeline = $redis->multi(Redis::PIPELINE);
        foreach ($accIdSet as $accId) {
            $pipeline->hMGet($accId, array("userId", "username", "appversion"));
        }
        return $pipeline->exec();

This returns the following array:

(
    [0] => Array
        (
            [userId] => user0234
            [username] => apples
            [appverion] => 1.0
        )

    [1] => Array
        (
            [userId] => user2342
            [username] => roses
            [appversion] => 2.0
        )
)

This is all good, except for the index of the array. The system I am working on requires the indexes to be the actual key stored in the Redis instead of a numerical index which it currently has.

Now I know I can iterate this array and use some PHP to change the index to the actual key, but before going there, I would like to know if I have a much more efficient and cleaner option to solve this problem.

I am open to any suggestion even if it changes the functions I use to interact with the Redis. Thank you!

Priyath Gregory
  • 927
  • 1
  • 11
  • 37

1 Answers1

0

I also encountered this problem, it seems can be resovle by redis itself directly. Maybe this is a workaround:

$data = $pipeline->exec();
foreach($data as $k => $v) {
    $data[$k]['accId1'] = $accIdSet[$k];
}
return $data;
HubQin
  • 1
  • 1