我们项目用的yii框架,链接了多个数据库。
数据库参数:
return [
'class' => 'yii\db\Connection',
'charset' => getenv('DB_CHAR'),
'enableSchemaCache' => true,
'schemaCacheDuration' => 3600,
'schemaCache' => 'schemaCache',
这几个参数大家都没注意,复制粘贴了多个库同样的配置。其实这里边有两个坑,被我们都踩到了。
1.如果enableSchemaCache = true 不指定schemaCache'参数,yii就会找'schemaCache' =>'cache' 。需要在config里配置名字为cache的缓存。例如:
'components' => [
'log' => require(__DIR__ . '/log.php'),
'cache' => 'yii\redis\Cache',
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => getenv('CACHE_ORM_SCHEME_HOST'),
'port' => getenv('CACHE_ORM_SCHEME_PORT'),
'database' => getenv('CACHE_ORM_SCHEME_DATABASE'),
// 'password' => getenv('CACHE_ORM_SCHEME_AUTH'),
],或者
'schemaCache' => [
'class' => 'yii\caching\MemCache',
'servers' => [
[
'host' => getenv('MEMCACHED_HOST_SCHEMA'),
'port' => getenv('MEMCACHED_PORT_SCHEMA'),
'weight' => getenv('MEMCACHED_WEIGHT_SCHEMA'),
]
],
],如果只有一个数据库,这样就可以了。但是,如果有多个数据库,还得注意1个参数。
return [
'dsn' =>'db0ne',
'class' => 'yii\db\Connection',
'charset' => 'utf8',
'enableSchemaCache' => true,
'schemaCacheDuration' => 3600,
'schemaCache' => 'schemaCache',一般我们配置数据库都很少配置dsn这个东西,不配置也没什么影响。但是,如果用到schema cache,就如果有出现问题。schema cache的key是怎么生成的,看了一下源码:
protected function getCacheKey($name)
{
return [
__CLASS__,
$this->db->dsn,
$this->db->username,
$name,
];
}__CLASS__ 这个每个数据库都一样。如果每个数据库的用户名也一样。虽然数据库名不一样,但是如果表名一样,就会出问题。$name就是表名!!!我们有个业务用到两个库,库名不一样,但是,表名一样,用户名一样,导致,表字段缓存成一样的了。。。2.看这两行代码:
$vipInfo = static::find()->where(['user_id' => $userId])->asArray()->one();
和
$vipInfo = static::find()->where(['user_id' => $userId])>one();
区别就是asArray(),如果用了asArray(),相当于直接返回数组,不返回ActiveRecord类结构。所以,就不会读取schema,就用不上schema cache。
private function createModels($rows)
{
$models = [];
if ($this->asArray) {
if ($this->indexBy === null) {
return $rows;
}
foreach ($rows as $row) {
if (is_string($this->indexBy)) {
$key = $row[$this->indexBy];
} else {
$key = call_user_func($this->indexBy, $row);
}
$models[$key] = $row;
}
} else {
/* @var $class ActiveRecord */
$class = $this->modelClass;
if ($this->indexBy === null) {
foreach ($rows as $row) {
$model = $class::instantiate($row);
$modelClass = get_class($model);
$modelClass::populateRecord($model, $row);
$models[] = $model;
}
} else {
foreach ($rows as $row) {
$model = $class::instantiate($row);
$modelClass = get_class($model);
$modelClass::populateRecord($model, $row);
if (is_string($this->indexBy)) {
$key = $model->{$this->indexBy};
} else {
$key = call_user_func($this->indexBy, $model);
}
$models[$key] = $model;
}
}
}
return $models;
}代码细节自己看吧。所以,如果不用asArray(),一定要设置enableSchemaCache,不会每次都会有:
SHOW FULL COLUMNS FROM `wx_edu_student`
SELECT
kcu.constraint_name,
kcu.column_name,
kcu.referenced_table_name,
kcu.referenced_column_name
FROM information_schema.referential_constraints AS rc
JOIN information_schema.key_column_usage AS kcu ON
(
kcu.constraint_catalog = rc.constraint_catalog OR
(kcu.constraint_catalog IS NULL AND rc.constraint_catalog IS NULL)
) AND
kcu.constraint_schema = rc.constraint_schema AND
kcu.constraint_name = rc.constraint_name
WHERE rc.constraint_schema = database() AND kcu.table_schema = database()
AND rc.table_name = 'myuser' AND kcu.table_name = 'myuser'
这种的查询,效率低下!!!要不就用asArray()!
本文探讨了使用Yii框架连接多个数据库时遇到的问题,特别是关于schema缓存的配置及使用方法,包括enableSchemaCache参数的正确配置方式以及如何避免因配置不当导致的数据一致性问题。

361

被折叠的 条评论
为什么被折叠?



