Browse Source

fix: #13749 Yii opens db connection even when hits query cache

tags/2.0.33
Ather Shu 5 years ago
parent
commit
7428a45bf9
  1. 2
      framework/CHANGELOG.md
  2. 9
      framework/db/Command.php
  3. 10
      framework/db/mysql/QueryBuilder.php

2
framework/CHANGELOG.md

@ -19,7 +19,7 @@ Yii Framework 2 Change Log
- Bug #17881: `yii\db\Query::queryScalar()` wasn’t reverting the `select`, `orderBy`, `limit`, and `offset` params if an exception occurred (brandonkelly)
- Bug #17875: Use `move_uploaded_file()` function instead of `copy()` and `unlink()` for saving uploaded files in case of POST request (sup-ham)
- Bug #17884: Fix 0 values in console Table rendered as empty string (mikehaertl)
- Bug #13749: Fix Yii opens db connection even when hits query cache (shushenghong)
2.0.32 January 21, 2020
-----------------------

9
framework/db/Command.php

@ -1140,8 +1140,7 @@ class Command extends Component
if (is_array($info)) {
/* @var $cache \yii\caching\CacheInterface */
$cache = $info[0];
$rawSql = $rawSql ?: $this->getRawSql();
$cacheKey = $this->getCacheKey($method, $fetchMode, $rawSql);
$cacheKey = $this->getCacheKey($method, $fetchMode);
$result = $cache->get($cacheKey);
if (is_array($result) && isset($result[0])) {
Yii::debug('Query result served from cache', 'yii\db\Command::query');
@ -1191,15 +1190,17 @@ class Command extends Component
* @return array the cache key
* @since 2.0.16
*/
protected function getCacheKey($method, $fetchMode, $rawSql)
protected function getCacheKey($method, $fetchMode)
{
ksort($this->params);
return [
__CLASS__,
$method,
$fetchMode,
$this->db->dsn,
$this->db->username,
$rawSql,
$this->getSql(),
json_encode($this->params),
];
}

10
framework/db/mysql/QueryBuilder.php

@ -390,7 +390,15 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/
private function supportsFractionalSeconds()
{
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
// use cache to prevent open mysql connection
// https://github.com/yiisoft/yii2/issues/13749#issuecomment-481657224
$key = [__METHOD__, $this->db->dsn];
$version = \Yii::$app->cache ? \Yii::$app->cache->get($key) : null;
if( !$version ) {
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
\Yii::$app->cache ? \Yii::$app->cache->set($key, $version) : null;
}
return version_compare($version, '5.6.4', '>=');
}

Loading…
Cancel
Save