Yii 1.1.6 集成安装 Rediscache

安装YiiRedis插件

 

目前主要有两种Yii插件:

 

Ø  Rediscache:基于predis(Redis的纯PHP实现客户端),无需安装Redis for PHP扩展。

 

Ø  YiiRedis:基于phpredis客户端,需要安装Redis for PHP扩展。

 

这里采用Rediscache插件,避免线上安装Redis for PHP扩展。

 

1、下载安装

 

从以下地址下载Rediscache插件:

 

http://www.yiiframework.com/extension/rediscache/files/redis.zip

 

 

将插件解压到 yii/dev/protected/extensions中:

 

插件文件部署后的位置应为: yii/dev/protected/extensions/redis/CredisCache.php

 

2、配置Rediscache

 

1 yii/dev/protected/config/main.php

 

===============================================================================

 

'components'=>array(

  .......

  .......

  'cache'=>array(

     'class'=>'ext.redis.CRedisCache',     //对应protected/extensions/redis/CredisCache.php

     'servers'=>array(

        array(

           'host'=>'192.168.1.50',

           'port'=>6379,

        ),

     ),

  ),

),

 

2、安装Yii的会话Redis插件

 

2.1、下载安装

 

GitHub上下载插件https://github.com/lincsanders/PRedisCacheHttpSession

 

 

解压到helloyii/app/protected/extensions下:

 

插件文件部署后的位置应为:

 

        yii/dev/protected/extensions/PredisCacheHttpSession/PRedisCacheHttpSession.php

 

2.2、配置PRedisCacheHttpSession

 

'components'=>array(

  .......

  .......

  'cache'=>array(

     'class'=>'ext.redis.CRedisCache',     //对应protected/extensions/redis/CredisCache.php

     'servers'=>array(

        array(

           'host'=>'192.168.1.50',

           'port'=>6379,

        ),

     ),

  ),

 

  'session'=>array(

     'class' =>'ext.PRedisCacheHttpSession.PRedisCacheHttpSession',

     'database' => 9,

  ),

),注意:缓存和会话的database属性一定要区分开,用不同的Redis数据库来保存。

 

3、编写控制器

 

编写一个读写缓存的控制器进行测试。

 

 

1 yii/dev/protected/controllers/CacheController.php

 

===============================================================================

 

class CacheController extends CController

 

{

 

public function actionFetch($key, $value)

 

 {

 

 Yii::app()->cache->set($key, $value);

 

 $data = Yii::app()->cache->get($key);

 

 Yii::app()->getController()->render('result',array('data'=>$data));

 

 }

 

}

 

 

2 yii/dev/protected/views/cache/result.php

 

===============================================================================

 

<?php

 

echo$data;

 

?>

 


你可能感兴趣的:(Yii 1.1.6 集成安装 Rediscache)