Browse Source

feat: update code after review

tags/2.0.33
Ather Shu 5 years ago
parent
commit
2051c05087
  1. 12
      framework/db/Command.php
  2. 14
      framework/db/mysql/QueryBuilder.php

12
framework/db/Command.php

@ -1140,7 +1140,7 @@ class Command extends Component
if (is_array($info)) {
/* @var $cache \yii\caching\CacheInterface */
$cache = $info[0];
$cacheKey = $this->getCacheKey($method, $fetchMode);
$cacheKey = $this->getCacheKey($method, $fetchMode, $rawSql);
$result = $cache->get($cacheKey);
if (is_array($result) && isset($result[0])) {
Yii::debug('Query result served from cache', 'yii\db\Command::query');
@ -1148,6 +1148,7 @@ class Command extends Component
}
}
}
$rawSql = $rawSql ?: $this->getRawSql();
$this->prepare(true);
@ -1186,13 +1187,14 @@ class Command extends Component
* @param string $method method of PDOStatement to be called
* @param int $fetchMode the result fetch mode. Please refer to [PHP manual](https://secure.php.net/manual/en/function.PDOStatement-setFetchMode.php)
* for valid fetch modes.
* @param string $rawSql the raw SQL with parameter values inserted into the corresponding placeholders
* @param string $rawSql Deprecated since 2.0.33, the raw SQL with parameter values inserted into the corresponding placeholders
* @return array the cache key
* @since 2.0.16
*/
protected function getCacheKey($method, $fetchMode)
protected function getCacheKey($method, $fetchMode, $rawSql)
{
ksort($this->params);
$params = $this->params;
ksort($params);
return [
__CLASS__,
$method,
@ -1200,7 +1202,7 @@ class Command extends Component
$this->db->dsn,
$this->db->username,
$this->getSql(),
json_encode($this->params),
json_encode($params),
];
}

14
framework/db/mysql/QueryBuilder.php

@ -9,6 +9,7 @@ namespace yii\db\mysql;
use yii\base\InvalidArgumentException;
use yii\base\NotSupportedException;
use yii\caching\CacheInterface;
use yii\db\Exception;
use yii\db\Expression;
use yii\db\Query;
@ -393,10 +394,19 @@ class QueryBuilder extends \yii\db\QueryBuilder
// use cache to prevent open mysql connection
// https://github.com/yiisoft/yii2/issues/13749#issuecomment-481657224
$key = [__METHOD__, $this->db->dsn];
$version = isset(\Yii::$app->cache) ? \Yii::$app->cache->get($key) : null;
$cache = null;
if ( $this->db->enableSchemaCache ) {
$schemaCache = is_string($this->db->schemaCache) ? \Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
if ($schemaCache instanceof CacheInterface) {
$cache = $schemaCache;
}
}
$version = $cache ? $cache->get($key) : null;
if( !$version ) {
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
isset(\Yii::$app->cache) ? \Yii::$app->cache->set($key, $version) : null;
if( $cache ) {
$cache->set($key, $version, $this->db->schemaCacheDuration);
}
}
return version_compare($version, '5.6.4', '>=');

Loading…
Cancel
Save