Commit 613d3071 by Qiang Xue

Fixes #5106: Refactored query caching to not load cache component when query…

Fixes #5106: Refactored query caching to not load cache component when query caching is not used at all.
parent d6b7a5e0
......@@ -778,27 +778,6 @@ class Command extends Component
}
/**
* Returns the effective query cache information.
* @return array the current query cache information, or null if query cache is not used.
*/
private function getQueryCacheInfo()
{
$info = $this->db->getQueryCacheInfo();
if (is_array($info)) {
if ($this->queryCacheDuration !== null) {
$info[1] = $this->queryCacheDuration;
}
if ($this->queryCacheDependency !== null) {
$info[2] = $this->queryCacheDependency;
}
if ($info[1] !== null && $info[1] >= 0) {
return $info;
}
}
return null;
}
/**
* Performs the actual DB query of a SQL statement.
* @param string $method method of PDOStatement to be called
* @param integer $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
......@@ -813,7 +792,7 @@ class Command extends Component
Yii::info($rawSql, 'yii\db\Command::query');
if ($method !== '') {
$info = $this->getQueryCacheInfo();
$info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency);
if (is_array($info)) {
/* @var $cache \yii\caching\Cache */
$cache = $info[0];
......
......@@ -465,22 +465,38 @@ class Connection extends Component
/**
* Returns the current query cache information.
* This method is used internally by [[Command]].
* @param integer $duration the preferred caching duration. If null, it will be ignored.
* @param \yii\caching\Dependency $dependency the preferred caching dependency. If null, it will be ignored.
* @return array the current query cache information, or null if query cache is not enabled.
* @internal
*/
public function getQueryCacheInfo()
public function getQueryCacheInfo($duration, $dependency)
{
if (!$this->enableQueryCache) {
return null;
}
$info = end($this->_queryCacheInfo);
if ($this->enableQueryCache) {
if (is_array($info)) {
if ($duration === null) {
$duration = $info[0];
}
if ($dependency === null) {
$dependency = $info[1];
}
}
if ($duration === 0 || $duration > 0) {
if (is_string($this->queryCache) && Yii::$app) {
$cache = Yii::$app->get($this->queryCache, false);
} else {
$cache = $this->queryCache;
}
if ($cache instanceof Cache) {
return is_array($info) ? [$cache, $info[0], $info[1]] : [$cache, null, null];
return [$cache, $duration, $dependency];
}
}
return null;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment