Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[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



946
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...