Home >Backend Development >PHP Tutorial >Phalcon framework, how to switch redis libraries

Phalcon framework, how to switch redis libraries

WBOY
WBOYOriginal
2016-12-01 01:27:362776browse

modelsCache is configured as follows

<code>$di->set('modelsCache', function () use ($config) {
    $cache = new Phalcon\Cache\Backend\Redis(xx, xx);
    return $cache;
});</code>

When injecting the service, the configuration parameter index can choose to specify the library storage. Check the API and see that there is a setOptions function. I tried to run the following code
and wanted to switch from the current db0 to db1, but all the data They are all saved to db0, but there is no switch. What should I do to switch databases at any time? ?

<code>$this->modelsCache->save('data_in_db0','value');
$opt=$this->modelsCache->getOptions();
$opt['index']=1;
$this->modelsCache->setOptions($opt);
$this->modelsCache->save('data_in_db1','value');</code>

Reply content:

modelsCache is configured as follows

<code>$di->set('modelsCache', function () use ($config) {
    $cache = new Phalcon\Cache\Backend\Redis(xx, xx);
    return $cache;
});</code>

When injecting the service, the configuration parameter index can choose to specify the library storage. Check the API and see that there is a setOptions function. I tried to run the following code
and wanted to switch from the current db0 to db1, but all the data They are all saved to db0, but there is no switch. What should I do to switch databases at any time? ?

<code>$this->modelsCache->save('data_in_db0','value');
$opt=$this->modelsCache->getOptions();
$opt['index']=1;
$this->modelsCache->setOptions($opt);
$this->modelsCache->save('data_in_db1','value');</code>

Encapsulate the redis operation class by yourself

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn