We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Redis: "Fatal error: Class 'redis' not found"

Hi all,

I'm trying to use Redis to manage cache and the php sessions, but I'm encountering a weird issue. I've traced the problem down to the save function. I'm testing the following code:


$oFrontCache = new Phalcon\Cache\Frontend\Data(array('lifetime' => 172800));
$oRedis = new Phalcon\Cache\Backend\Redis($oFrontCache, array('redis' => $asRedisConf));

echo '<pre>';
var_dump($oRedis);         //everything is fine here
echo '</pre>';

$oRedis->save('my-data', array(1, 2, 3, 4, 5));          //Fatal error: Class 'redis' not found ... 

I'm using Phalcon 2.0.0, I can't see any reason for that and can't find any workaround. I'd appreciate any help on this.

Cheers

try with \Phalcon...

    <?php

 // Cache data for 2 days
 $frontCache = new \Phalcon\Cache\Frontend\Data(array(
    "lifetime" => 172800
 ));

 //Create the Cache setting redis connection options
 $cache = new \Phalcon\Cache\Backend\Redis($frontCache, array(
    'host' => 'localhost',
    'port' => 6379,
    'auth' => 'foobared',
    'persistent' => false
 ));

 //Cache arbitrary data
 $cache->save('my-data', array(1, 2, 3, 4, 5));

 //Get data
 $data = $cache->get('my-data');


24.8k

Hi,

Thanks foryour answer. I've tried, but it didn't change anything. I ruled it out yesterday using the following code:

var_dump( class_exists('Phalcon\Cache\Backend\Redis'));
var_dump( class_exists('\Phalcon\Cache\Backend\Redis'));

To be clear, the $oRedis variable is a proper redis object. The problem only occurs when I try to use methods (_connect, save, get... ).

object(Phalcon\Cache\Backend\Redis)#37 (8) {
  ["_frontend":protected]=>
  object(Phalcon\Cache\Frontend\Data)#19 (1) {
    ["_frontendOptions":protected]=>
    array(1) {
      ["lifetime"]=>
      int(172800)
    }
  }
  ["_options":protected]=>
  array(5) {
    ["redis"]=>
    array(4) {
      ["host"]=>
      string(9) "localhost"
      ["port"]=>
      int(6379)
      ["auth"]=>
      string(11) "xyzxyzxyz"
      ["persistent"]=>
      bool(false)
    }
    ["host"]=>
    string(9) "127.0.0.1"
    ["port"]=>
    int(6379)
    ["persistent"]=>
    bool(false)
    ["statsKey"]=>
    string(5) "_PHCR"
  }
  ["_prefix":protected]=>
  string(0) ""
  ["_lastKey":protected]=>
  string(0) ""
  ["_lastLifetime":protected]=>
  NULL
  ["_fresh":protected]=>
  bool(false)
  ["_started":protected]=>
  bool(false)
  ["_redis":protected]=>
  NULL
}

That's a really odd one, don't know where the problem might come from. Cheers

i works with redis for sessions: i do this: in my index.php or service.php

  $di->setShared('session', function() {
  $session = new Phalcon\Session\Adapter\Redis(array(
      'path' => "tcp://127.0.0.1:6379?weight=1",
      'name' => SITE_DOMAIN . '-'
  ));

  $session->start();

  return $session;
});

And in my controller:

$this->session->set('string_to_save', $variable);

for to obtein var session:

$this->session->get('string_to_save');

Try..



24.8k

Seems like I had a few different problems: igBinary module was not working properly, and the auth parameter doesn't seem to work. So fixed those 2 issues, but I'd like to push it a bit further...

I want to use Redis for the php sessions and my frontend cache. To make sure those 2 are properly separated, I'd like to use different redis DB but I can't seem to find the right way to set that up.

Now, I've tried to add extra options for my redis connections todefine specific Redis db, prefix, serializer... So I tried to use $redis->select(DB_NUMBER) , but that doesn't work. Tried to pass extra param in the Option array, not better. Here is the code I'm currently trying:

$___asConfig['session']= array(
    'path' => 'tcp://127.0.0.1:6379?weight=1',
    'name' => '',
    'lifetime' => 43200,    //12hr
    'cookie_lifetime' => 0, // infinite
    'cookie_secure' => true,
    Redis::OPT_SERIALIZER => Redis::SERIALIZER_IGBINARY,
    Redis::OPT_PREFIX => '___projectID-Session__'
);

$di->setShared('session', function(){

        $oRedis = new Phalcon\Session\Adapter\Redis($___asConfig['session']);
        //$oRedis->select(CONF_REDIS_DB_SESSION);
        $oRedis->start();
        return $oRedis;
    });

and for the cache


    $asRedisConf = array(
    'host' => 'localhost',
    'port' => 6379,
    'auth' => 'daughter123',  // doeasn't work 'auth' => 'daughter123',
    'persistent' => false
);

    $oCache = function () use($asRedisConf) {
    $oFrontCache = new Phalcon\Cache\Frontend\Data(array(
    'lifetime' => 36000,
    'prefix'   => 'fe.'
    ));

    $oRedis = new Phalcon\Cache\Backend\Redis($oFrontCache, array('redis' => $asRedisConf));
    //$oRedis->select(CONF_REDIS_DB_CACHE);
    return $oRedis;
};

$di->setShared('cache', $oCache);
$di->setShared('viewCache', $oCache);

Has anyone tried to dig deep into Redis config and adapaters ? Thanks

edited May '15

for cache i used to Igbinary : first install the extension: sudo apt-get install php5-igbinary ... and in services:

    //Set the views cache service for fragments (templates volt):

$di->set('viewCache', function() use ($config) {
    $frontCache = new Phalcon\Cache\Frontend\Output(array(
        "lifetime" => $config->application->viewCacheLifeTime
    ));
    $cache = new Phalcon\Cache\Backend\Memcache($frontCache, array(
        "host" => "localhost",
        "port" => "11211",
        'persistent' => false
    ));
    return $cache;
});

//Set the data cache service for php functions, etc, db queries:

$di->set('dataCache', function() use ($config) {
    $frontCache = new Phalcon\Cache\Frontend\Igbinary(array(
        "lifetime" => $config->application->dataCacheLifeTime
    ));
    $cache = new Phalcon\Cache\Backend\Apc($frontCache, array(
        "prefix" => 'apccache'
    ));
    return $cache;
});


24.8k

Can I ask you what the "$dispatcher->setEventsManager($di->get('eventsManager'));" lines are used for ?

I've found another example that was recommending another approach:

$di->setShared('modelsCache', function () use($asRedisConf) {
    return new \Phalcon\Mvc\Model\MetaData\Redis(array(
    'lifetime' => 3600,
    'prefix'   => 'models.',
    'redis'    => $asRedisConf
    ));
});

$di->setShared('modelsMetadata', function () use($asRedisConf) {
    return new \Phalcon\Mvc\Model\MetaData\Redis(array(
    'lifetime' => 3600,
    'prefix'   => 'models.meta.',
    'redis'    => $asRedisConf
    ));
});

What's the difference ? Is your dataCache a generic cache or something dedicated to the models ? Cheers

Sorry, i update the code,

$dispatcher->setEventsManager($di->get('eventsManager'));

is for other purpose and i was testing.

dataCache i'll use for cache php functions, database querys in controllers, all in controllers, and save in apc cache..

im not use modelsMetadata yet...



24.8k

I see, thanks for all your help. I'll keep looking for a way to select different Redis DB, as it may be useful for other people too.



24.8k

UPDATE: for anyone interested in redis, here is the answer. Found the anwer looking at the .zep file on github.


$asRedisConf = array(
    'host' => 'localhost',
    'port' => 6379,
    'auth' => 'daughter123',  // doeasn't work 'auth' => 'daughter123',
    'persistent' => false,
    'statsKey' => '_dfe_',
    'index' => 1
);

$oCache = function () use($asRedisConf) {

    //$oFrontCache = new Phalcon\Cache\Frontend\Output(array(
    //$oFrontCache = new Phalcon\Cache\Frontend\Data(array(
    $oFrontCache = new Phalcon\Cache\Frontend\Igbinary(array(
    'lifetime' => 36000,
    'prefix'   => 'fe.'
    ));

    $oRedis = new Phalcon\Cache\Backend\Redis($oFrontCache, array('redis' => $asRedisConf));
    return $oRedis;
};

$di->setShared('cache', $oCache);

Next step for me: trying to make Volt use Redis too: there's no point writing html fragments on the drive if I'm using redis for everything else.

Cheers