Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Redis in CodeIgniter?

I get the tutorial in:

http://yaminnoor.com/redis-codeigniter/

https://codeigniter.com/user_guide/libraries/caching.html#redis

I try it like this:

Config (application\config\redis.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'; //change this to match your amazon redis cluster node endpoint
$config['password'] = NULL;
$config['port'] = 6379;
$config['timeout'] = 0;

Controller:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Redis_tes extends CI_Controller {
    public function __construct() {
        parent::__construct();

        $this->load->driver('cache', array('adapter' => 'redis', 'backup' => 'file'));
    }

    public function index() {
        // die('tes');
        if($this->cache->redis->is_supported() || $this->cache->file->is_supported()) {
            $var1 = $this->cache->get('cache_var1');
        } else {
            $cache_var1 = $this->_model_data->get_var1();
            $this->cache->save('cache_var1', $cache_var1);
        }
    }
}
?>

I run http://localhost/app_redis/redis_tes, which produces the following error:

An Error Was Encountered

Invalid driver requested: CI_Cache_redis

Any solution to solve my problem?

like image 806
moses toh Avatar asked Nov 08 '22 18:11

moses toh


1 Answers

Look here: https://github.com/joelcox/codeigniter-redis Try to use this library.

Update : This library is deprecated. Author recommends to migrate on Predis.

like image 158
Alex Dom Avatar answered Nov 14 '22 21:11

Alex Dom