用YII2 开发了一套程序,牵扯到的数据非重要长期缓存的放单机redis中,重要的放集群中
使用composer安装predis扩展:
composer require mojifan/yii2-predis
在配置文件添加如下配置:
'redis_single' => [
'class' => 'mojifan\redis\Connection',
'servers'=>[
['host' => '127.0.0.1','port'=> 6379],
],
],
'redis_cluster' => [
'class' => 'mojifan\redis\Connection',
'servers'=>[
['host' => '127.0.0.1','port'=> 6380],
['host' => '127.0.0.1','port'=> 6381],
['host' => '127.0.0.1','port'=> 6382],
],
'options'=>['cluster' => 'redis'],
],
最后可以使用了
$redis = Yii::$app->redis_single;
$redis->set('site', 'architecy.com');
$value = $redis->get('site');
echo 'The value of site in redis is: ', $value;
查看原文:http://www.architecy.com/archives/335