How To Use Yii2/redis




$redis = new \Redis();

$redis->connect('localhost','9002');

$redis->select(0);

$redis->setOption(\Redis::OPT_PREFIX,'myprefix');

$redis->hmset('key',['name'=>'joe','salary'=>2000]);



How to use redis->hmset with yii2/redis like above?

The following code does not work!




'components' => [

            'redis'=>[

                'class'=>'yii\redis\Connection',

                'hostname'=>'127.0.0.1',

                'port' => 9002,

                'database' => 1,

            ],

        ],




\Yii::$app->redis->hmset('key',['name'=>'joe','salary'=>2000]);

How exactly it does not work?


\Yii::$app->redis->hmset('key',['name'=>'joe','salary'=>2000]);

PHP Warning – yii\base\ErrorException

mb_strlen() expects parameter 1 to be string, array given

yii\redis\Connection line 357

simply serialize your array


\Yii::$app->redis->hmset('key',serialize(['name'=>'joe','salary'=>2000]));

If the array is serialized,how can I get the value by using


$redis->hmGet('key', array('name'));