Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can predis hmget use array as parameter of multiple fields

Tags:

php

redis

predis

can predis use array as 2nd parameter of hmget() to retrieve multiple fields on one go? e.g. $client->hmget($key, $fields); //$fields is an array

Can it also accept many parameters of string as fields? e.g.: $client->hmget($key, $field1, $field2, $field3);

like image 978
Will-i-Am-Davidon Avatar asked Feb 13 '14 03:02

Will-i-Am-Davidon


2 Answers

Predis supports two ways of passing multiple keys (or keys with values) for variadic Redis commands. The first one basically follows the same signature of commands as defined by the Redis documentation, so using HMSET and HMGET as examples you will have:

$redis->hmset("hash", "field:1", "value:1", "field:2", "value:2");
$values = $redis->hmget("hash", "field:1", "field:2");

but you can also pass the list of keys and/or values as a single array argument:

$redis->hmset("hash", array("field:1" => "value:1", "field:2" => "value:2"));
$values = $redis->hmget("hash", array("field:1", "field:2"));

Choosing which one to use is really just a matter of preference.

like image 106
nrk Avatar answered Nov 01 '22 01:11

nrk


From Predis repository

$redis->hmset('metavars', 'foo', 'bar', 'hoge', 'piyo', 'lol', 'wut');

$redis->hmget('metavars', 'foo', 'hoge', 'unknown'));
like image 32
Jack Daniel's Avatar answered Nov 01 '22 00:11

Jack Daniel's