Session & Cache in Redis

Hi all,

I'm building an app that rely on Redis to store cache and session but I'm having a few issues. Nothing really critical, but some options/parameters seem to be missing in the adapter. I've updated Phalcon and the incubator this morning.

First of all, I've install phpredis and igbinary and my app works just fine. But I'd like to push the configuration a bit further: - using different redis.databases for session and different caches - using different preffix for different type of cache ( "fe." for frontend, "be." for backend, "data." for generated sets of data...)

front & backend cache:

I'm using the default phalcon adapter here (Phalcon\Cache\Backend\Redis) and I can't find the way to customize the prefix or database used by Redis.

Tried many sets of config, but can't make redis use the prefix or database I want. See a sample below:

$asRedisConf = array(
    'host' => '127.0.0.1:6379?weight=1&database='.CONF_REDIS_DB_CACHE,
    'port' => 6379,
    'persistent' => false,
    'index' => CONF_REDIS_DB_CACHE,
    'database' => CONF_REDIS_DB_CACHE,

    'lifetime' => 43200,

    'prefix'   => '_dfe_',
    'uniqueId' => '_dfe_',
    'statsKey' => '_dfe_',
    'prefix' => '_dfe_',
    'name' => '_dfe_',

    'index' => CONF_REDIS_DB_SESSION,
    'database' => CONF_REDIS_DB_SESSION
);

$di->set('cache', function () use($asRedisConf) {

    $oFrontCache = new Phalcon\Cache\Frontend\Data(array(
    'lifetime' => 43200,
    'prefix'   => 'all.'
    ));

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

Sessions

I've got roughly the same problems. I'm using the Redis class from the incubator here. I've found a way to specify a database in the connection url, but couldn't specify any prefix.

$___asConfig['session'] = array(
    'path' => 'tcp://127.0.0.1:6379?weight=1&database='.CONF_REDIS_DB_SESSION,
    'lifetime' => 43200,   //12hr
    'cookie_lifetime' => 0,    // infinite
    'cookie_secure' => true,

    'uniqueId' => '_dses_',
    'statsKey' => '_dses_',
    'prefix' => '_dses_',
    'name' => '_dses_',

    'index' => CONF_REDIS_DB_SESSION,
    'database' => CONF_REDIS_DB_SESSION
);

$di->get('loader')->registerClasses(array('Phalcon\Session\Adapter\Redis' => PATH_LIBS.'/PIncubator/Session/Adapter/Redis.php'), true);
    $di->setShared('session', function() use($___asConfig){

        $oRedis = new \Phalcon\Session\Adapter\Redis($___asConfig['session']);

        //$oRedis->select(CONF_REDIS_DB_SESSION);
        //->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); 

        $oRedis->start();
        return $oRedis;
    });

Has anybody managed to make that work ? Should I give up Phalcon class and use phpRedis directly ? Or is there any plan to update the adapters to allow custom connections params (selectDb(), setPrefix()...)

Thanks



10.0k

nobody ? Really :( I can't believe I'm the only one here trying to use advanced settings for Redis.



9.9k

Hi Stephane, did you ever find a solution for this? I am searching for the same info!



10.0k

Hi Al,

To be honest I haven't looked into that for quite a while now. As said in my original post, it's not critical, and we can always prefix keys to Identify the "scope". And since it doesn't seem to impact Redis performances I've let things as it was.

But phalcon has received a few updates since then, the incubator too, so I may have another look soon to see if that's been fixed Cheers

Hi Stephane, did you ever find a solution for this? I am searching for the same info!



9.9k
edited Feb '16

I worked it out last night after posting that reply. Here's my config with the correct keys for selecting db and prefix. It all seems to work ok, but like you said the versions have changed;

[session]
adapter = "Redis"
options[host] = "127.0.0.1"
options[port] = "6379"
options[index] = "0" ; select db
options[lifetime] = 360
options[uniqueId] = "ba_site"
options[prefix] = "_ba_Sess_" ; prefix
options[persistent] = true


10.0k

Just had a look, and my version works too now. Changed the values of my constant (CONFREDISDB_SESSION), and saw new databases to being used (redis-cli INFO keyspace).

I guess it's been fixed in a recent version. Problem solved.

Got a question for you now :) Is the persistent option working ? Have you tested the behaviour if set to false ? Cheers

I worked it out last night after posting that reply. Here's my config with the correct keys for selecting db and prefix. It all seems to work ok, but like you said the versions have changed;

[session] adapter = "Redis" options[host] = "127.0.0.1" options[port] = "6379" options[index] = "0" ; select db options[lifetime] = 360 options[uniqueId] = "basite" options[prefix] = "baSess" ; prefix options[persistent] = true



9.9k

I'm not sure it is. I am having some session problems since switching to redis.