CI3提供了使用Redis的配置,不过需要通过PHP的Redis扩展phpredis来使用链接Redis
官网这样说:
Redis is an in-memory key-value store which can operate in LRU cache mode. To use it, you need Redis server and phpredis PHP extension.
phpredis是用C语言编写的PHP扩展,效率比较高。
既然是PHP扩展,那就得先安装了,之前有写过安装扩展的办法,PHP安装pdo_mysql扩展,PHP安装mysqli扩展
这里说下安装过程
1,下载phpredis源码,解压放到PHP源码的扩展目录/home/php-5.6.8/ext
2,进入phpredis的解压目录编译安装
# /usr/local/php/bin/phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make & make install
3,修改php.ini,添加redis.so,并重启php-fpm和nginx
4,打开phpinfo,可以看到redis扩展了

下面介绍CodeIgniter3中配置Redis
1,在application/config添加配置文件redis.php,内容如下:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $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;
2,在控制器中使用Redis
public function set() { $username = $this->input->post('username',TRUE); $this->load->driver('cache'); // 数据保存到Redis $this->cache->redis->save('username', $username, 300); } public function get() { $this->load->driver('cache'); // 获取Redis数据 $username = $this->cache->redis->get('username'); $data = array('username' => $username); }
更多用法,请查看官网:https://www.codeigniter.com/userguide3/libraries/caching.html#redis-caching
Leave a Reply