scvmm 发表于 2018-11-4 13:30:25

codeigniter 3.X使用redis

  学习redis正好用codeigniter来练习
  CI3.X自带redis库并且在两个地方使用了这个功能,前提系统安装phpredis 这个PHP扩展
  1、储存session的驱动支持redis
  http://codeigniter.org.cn/user_guide/libraries/sessions.html?highlight=redis
  设置application/config/config.php
$config['sess_driver'] = 'redis';  
$config['sess_save_path'] = 'tcp://localhost:6379';
  使用同session的使用,具体见手册
$this->load->library('session');  这种方法只是使用redis来储存session
  2、CI的缓存驱动器(Caching Driver)
  http://codeigniter.org.cn/user_guide/libraries/caching.html?highlight=redis#redis
  CI3.X支持多种缓存方式,redis只是其中一种,不过CI将redis和其他缓存方式都放到“cache”这个驱动模块中
$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));  CI缓存使用方法见手册
  具体说说redis缓存的使用
  修改 application/config/redis.php 设置
$config['socket_type'] = 'tcp'; //`tcp` or `unix`  
$config['socket'] = '/var/run/redis.sock'; // in case of `unix` socket type
  
$config['host'] = '127.0.0.1';
  
$config['password'] = NULL;
  
$config['port'] = 6379;
  
$config['timeout'] = 0;
  使用
$this->load->driver('cache');  
$this->cache->redis->save('foo', 'bar', 10);
  redis缓存在CI中的设置十分简单,BUT,
  可能是因为redis只是缓存驱动的一种,CI能实现的功能也十分简单。
  源码 system/libraries/Cache/drivers/Cache_redis.php的111行
$this->_redis = new Redis();  CI并没继承phpredis的类,所以cache对redis的操作进行了封装,看封装的几个方法,CI的redis驱动只支持简单的字符串类型
  codeigniter-redis第三方驱动
  https://github.com/joelcox/codeigniter-redis
  看更新时间,还是CI 2.X时候开发的,不过phpredis没有太大升级,CI 3.X用起来应该也没什么影响
  安装也很简单
  将Redis.php类库放到system/libraries下
  application/config/autoload.php 加载类库,添加
$autoload['libraries'] = array('redis');  在 application/config/redis.php
  添加配置
$config['redis_default']['host'] = '127.0.0.1';      // IP address or host  
$config['redis_default']['port'] = '6379';            // Default Redis port is 6379
  
$config['redis_default']['password'] = '';            // Can be left empty when the server does not require AUTH
  

  
$config['redis_slave']['host'] = '127.0.0.1';
  
$config['redis_slave']['port'] = '6379';
  
$config['redis_slave']['password'] = '';
  现在就可以用CI类库的方式使用redis了
  测试
    $this->load->driver('redis');  
    $array_mset=array(
  
                  'first_key'=>'first_val',
  
                  'second_key'=>'second_val',
  
                  'third_key'=>'third_val'
  
    );
  
    $this->redis->mset($array_mset); #用MSET一次储存多个值
  
    $array_mget=array('first_key','second_key','third_key');
  
    var_dump($this->redis->mget($array_mget));
  
    #一次返回多个值 //array(3) { => string(9) "first_val" => string(10) "second_val" => string(9) "third_val" }
  不过这里有个冲突,加载第三方redis类库后,原生的cache无法使用redis模块,
  因为第三方redis类库的config和CI 3.X的redis驱动config的结构不同,加载方式也不同
  第三方autoload时
$this->_ci->load->config('redis');  而Cache_redis.php是
$CI->config->load('redis', TRUE, TRUE)  所以造成cache无法使用redis模块。
  (测试CI的autoload加载模块先加载,默认模块是调用时候加载)
  解决方案,修改cache的redis配置,放到一个redis数组中
$config['redis']['socket_type'] = 'tcp'; //`tcp` or `unix`  
$config['redis']['socket'] = '/var/run/redis.sock';
  
$config['redis']['host'] = '127.0.0.1';
  
$config['redis']['password'] = NULL;
  
$config['redis']['port'] = 6379;
  
$config['redis']['timeout'] = 0;
  其实个人觉得没这个必要,如果使用了第三方redis类库没必要同时使用cache模块。


页: [1]
查看完整版本: codeigniter 3.X使用redis