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 database number] select number of database for Redis

Hi,All

I have a question #########How to select number of database for Redis in Phalcon framework?#########

Firstly,I can use follow code to connect Redis server.

    $di = new \Phalcon\DI\FactoryDefault();
    $di->setShared('redis', function (){
        $redis = new \Redis();
        $redis->connect("localhost");
        $redis->select(1);    // I need use no. 1 database of Redis
        return $redis;
        });

Anywhere In controller file,I can use the variable($this->redis) to 'get' or 'set' Redis data! of course,I can use method named 'select' to change number of database.

    $this->redis->select(2);
Howover

In the Phalcon framework,I can use follow code to define redis connection!

    $frontCache = new \Phalcon\Cache\Frontend\Data([
        'lifetime' => -1
    ]);
    $config = [
        'host' => '127.0.0.1',
        'port' => 6379,
    ];
    $redis = new \Phalcon\Cache\Backend\Redis($frontCache, $config);

But,it seems that the class '\Phalcon\Cache\Backend\Redis' didn't has options and methods to connect other database of Redis!

Please,Help Me,Thanks



5.0k
Accepted
answer
edited Jun '16

SOLVED

    $frontCache = new \Phalcon\Cache\Frontend\Data([
        'lifetime' => -1,
        'prefix' => "_Qvil_"
    ]);
    $config = [
        'host' => '127.0.0.1',
        'port' => 6379,
        'index' => 1,
    ];
    $redis = new \Phalcon\Cache\Backend\Redis($frontCache, $config);

In the variable $frontCache,We can set lifetime and prefix key of cache,But,Phalcon use __PHCR as prefix,

so the prefix is __PHCR_Qvil_

In the variable $config,We can use keyword 'index' to set database for redis...