Yii2 使用十八 使用redis
2015-06-27 14:04
661 查看
Yii2的Redis包含两种存储方式:
CacheSession
安装
使用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);
相关文章推荐
- Redis命令学习-Pub/Sub(发布/订阅)
- 用Redis实现分布式锁
- Redis 笔记与总结5 Redis 常用命令之 键值命令 和 服务器命令 && 高级应用之 安全性 和 主从复制
- 超强、超详细Redis数据库入门教程
- Redis安装
- redis 管道
- centos6.5 负载均衡(nginx) session共享(redis)
- redis事务详解
- spring+redis整合入门
- PHP使用redis
- Redis 事务
- Redis有序集内部实现原理分析(二)
- 使用twemproxy部署redis集群
- redis的一点用法
- redis cluster 的手工配置
- redis info参数详解
- Redis事务操作
- Redis 字典的实现(1)
- Redis 笔记与总结4 set 和 zset 类型
- golang sortedset 实现(基于redis skiplist)