framework
framework copied to clipboard
缓存组件redis使用长连接后,0号库读写存在问题
所属功能组件
缓存(Cache)
ThinkPHP 版本
6.0
操作系统
Ubuntu
错误信息
cache.php配置如下
return [
'default' => 'redis',
'stores' => [
// 文件缓存
'file' => [
// 驱动方式
'type' => 'file',
// 设置不同的缓存保存目录
'path' => '../runtime/file/',
],
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => 'xxx',
'port' => '6379',
'password' => '',
'select' => 1,
'persistent' => true,
'prefix' => '',
'serialize' => true,
],
'test00' => [
'type' => 'redis',
'host' => 'xxx',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 0,
'persistent' => true,
'prefix' => '',
'serialize' => true,
]
],
];
控制器代码如下:
public function test()
{
\think\facade\Cache::store('test00')->set('test00', 'test00');
return 'success';
}
执行控制器代码,此时test00这个key写在了1号库没有按照配置写入0号库
另外修改配置,将persistent都改成false这时候正常写入,又或者将源码中的if判断注释,强制进行select,,则能正常读写
其它说明
No response
长连接模式下,如果之前已经选择了一个非0的数据库,那么后续的请求复用这个连接时,会保持之前选择的数据库。
可以这样优化一下:
// 选择指定数据库
if (isset($this->options['select']) && is_numeric($this->options['select'])) {
$this->handler->select($this->options['select']);
}