Phalcon 框架,redis 如何切换库

原创
2023-03-02 12:06:01 2282浏览

modelsCache配置如下

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

在注入服务时候,配置参数 index可选择指定库存储,查看 API 看到有个 setOptions 函数,试图远行下面代码
想从当前 db0,切换到 db1,但是所有数据都存到 db0 了,没有切换,应该怎么做才能随时换库??

$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');

回复内容:

modelsCache配置如下

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

在注入服务时候,配置参数 index可选择指定库存储,查看 API 看到有个 setOptions 函数,试图远行下面代码
想从当前 db0,切换到 db1,但是所有数据都存到 db0 了,没有切换,应该怎么做才能随时换库??

$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');

自己封装redis的操作类

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。