yii2-redis
yii2-redis copied to clipboard
redis hashes as php array
I propose to explicitly define next methods in \yii\redis\Connection
public function hSet($key, array $data);
public function hMset($key, array $data);
public function hGetAll($key);
To work with redis hashes as php associative arrays e.g.
#set
$redis->hMset('someKey', ['hello' => 'world', 'someKey' => 'someValue']);
#get
$redis->hGetAll('someKey');
#return ['hello' => 'world', 'someKey' => 'someValue'], instead of ['hello', 'world', 'someKey', 'someValue']
Good idea. @cebe would it break BC?
And not only hashes - zrange, zrevrange .etc with options WITHSCORES too
$redis->executeCommand('ZREVRANGE',['myzset', 0, 2, 'WITHSCORES']);
#return ['value1',5,'value2',4] best expected ['value1'=>5, 'value2'=>4]
Not completely clear but it may be implemented as separate method
this will break existing magic methods.
public function hSet($key, array $data);
It is recommended to
public function hSet($key, $data, $value = '');
to compatible $redis->hSet('somekey','field','value');
Hi, all~ You may simply add a helper function in your extended ArrayHelper etc.
/**
* post process for redis hgetall, rebind the key-value relation
* @param array $arr
* @param array $decodeFileds the filed names need json_decode
* @return NULL|[]
* @author xieyh
*/
public static function postRedisHgetall($arr, $decodeFileds = null) {
if(empty($arr)) return null;
$cnt = count($arr);
$data = [];
for($i = 0; $i < $cnt; $i += 2){
$filed = $arr[$i];
$value = $arr[$i+1];
if($decodeFileds && in_array($filed, $decodeFileds)){
$value = json_decode($value, TRUE);
}
$data[$filed] = $value;
}
return $data;
}
The feature of returning associative array is useful.