您的位置:首页 > 数据库 > Redis

Yii2 使用十八 使用redis

2015-06-27 14:04 661 查看

Yii2的Redis包含两种存储方式:

Cache

Session

安装

使用composer安装

推荐安装方式是使用composer。

在composer.json里的require节加上:

"yiisoft/yii2-redis": "~2.0.0"


修改配置文件

return [
//....
'components' => [
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
]
];


使用

下面使用函数就比较简单了

Yii::$app->redis->set('a','12345');


其它函数可以参考这里php使用redis的函数:

http://blog.csdn.net/xundh/article/details/46627949

有部分函数使用时要注意,比如mget:

$tempkeyarray=explode(',',$tempkey);
$resultarray = Yii::$app->redis->executeCommand('mget',$tempkeyarray);


问题处理

yii2-redis在使用时,有时会不稳定,经常报错,错误指向:

Connection.php的 386行:

/**
* @param string $command
* @return mixed
* @throws Exception on error
*/
private function parseResponse($command)
{
if (($line = fgets($this->_socket)) === false) {
throw new Exception("Failed to read from socket.\nRedis command was: " . $command);
}


解决方法:

修改php.ini,设置:

default_socket_timeout = -1

可以在index.php入口处加:

<?php
ini_set('default_socket_timeout', -1);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: