feat: update code after review

This commit is contained in:
Ather Shu
2020-02-27 08:05:28 +08:00
parent b9316c9205
commit 2051c05087
2 changed files with 19 additions and 7 deletions

View File

@ -1140,7 +1140,7 @@ class Command extends Component
if (is_array($info)) { if (is_array($info)) {
/* @var $cache \yii\caching\CacheInterface */ /* @var $cache \yii\caching\CacheInterface */
$cache = $info[0]; $cache = $info[0];
$cacheKey = $this->getCacheKey($method, $fetchMode); $cacheKey = $this->getCacheKey($method, $fetchMode, $rawSql);
$result = $cache->get($cacheKey); $result = $cache->get($cacheKey);
if (is_array($result) && isset($result[0])) { if (is_array($result) && isset($result[0])) {
Yii::debug('Query result served from cache', 'yii\db\Command::query'); 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); $this->prepare(true);
@ -1186,13 +1187,14 @@ class Command extends Component
* @param string $method method of PDOStatement to be called * @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) * @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. * 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 * @return array the cache key
* @since 2.0.16 * @since 2.0.16
*/ */
protected function getCacheKey($method, $fetchMode) protected function getCacheKey($method, $fetchMode, $rawSql)
{ {
ksort($this->params); $params = $this->params;
ksort($params);
return [ return [
__CLASS__, __CLASS__,
$method, $method,
@ -1200,7 +1202,7 @@ class Command extends Component
$this->db->dsn, $this->db->dsn,
$this->db->username, $this->db->username,
$this->getSql(), $this->getSql(),
json_encode($this->params), json_encode($params),
]; ];
} }

View File

@ -9,6 +9,7 @@ namespace yii\db\mysql;
use yii\base\InvalidArgumentException; use yii\base\InvalidArgumentException;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\caching\CacheInterface;
use yii\db\Exception; use yii\db\Exception;
use yii\db\Expression; use yii\db\Expression;
use yii\db\Query; use yii\db\Query;
@ -393,10 +394,19 @@ class QueryBuilder extends \yii\db\QueryBuilder
// use cache to prevent open mysql connection // use cache to prevent open mysql connection
// https://github.com/yiisoft/yii2/issues/13749#issuecomment-481657224 // https://github.com/yiisoft/yii2/issues/13749#issuecomment-481657224
$key = [__METHOD__, $this->db->dsn]; $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 ) { if( !$version ) {
$version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_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', '>='); return version_compare($version, '5.6.4', '>=');