diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6048a1c..fe760f3 100644 --- a/framework/CHANGELOG.md +++ b/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 ----------------------- diff --git a/framework/db/Command.php b/framework/db/Command.php index 6735607..5f487ab 100644 --- a/framework/db/Command.php +++ b/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), ]; } diff --git a/framework/db/mysql/QueryBuilder.php b/framework/db/mysql/QueryBuilder.php index 14d729b..b938fbb 100644 --- a/framework/db/mysql/QueryBuilder.php +++ b/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', '>='); }