(1)准备
A. 安装好redis
https://github.com/MSOpenTech/redis 注意:下载release版
启动脚本如下:
redis-server redis.windows.conf
双击打开 redis-cli.exe ,用自带的客户端工具进行测试
127.0.0.1:6379> set aaa xyz OK 127.0.0.1:6379> get aaa "xyz" 127.0.0.1:6379>
B. php5.6的redis扩展
redis扩展下载
http://windows.php.net/downloads/pecl/snaps/redis/
将php_redis.dll放入php的ext目录下
修改php.ini,加入
extension=php_redis.dll
重启apache,查看phpinfo就有redis扩展的信息了
(2)应用
yii的advanced应用
下载redis扩展, 下载
放到vender\yiisoft\yii2
common\config\main.php
'session' => [ 'class' => 'yii\redis\Session', 'redis' => [ 'hostname' => 'localhost', 'port' => 6379, 'password' => 'abc123', 'database' => 0, ], ],
Yii::$app->session->set('test1', '126');
若是在代码中使用Yii::$app->redis->get('key')会出错
更改配置
'components' => [ 'redis' => [ 'class' => 'yii\redis\Connection', 'hostname' => 'localhost', 'port' => 6379, 'password'=>'abc123', 'database' => 0, ], 'session' => [ 'class' => 'yii\redis\Session', 'redis' =>'redis', ], ],
注:password是abc123设置方法
在redis客户端
先查看
config get requirepass
设置
config set requirepass abc123
授权
auth abc123
(3)测试
public function actionTest(){
Yii::$app->session->set('test1', '12566'); }
redis客户端查询
执行Yii的demo
使用yii自带的debug