mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-11 19:20:01 +08:00
Fixes #5106: Refactored query caching to not load cache component when query caching is not used at all.
This commit is contained in:
@@ -777,27 +777,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.
|
* Performs the actual DB query of a SQL statement.
|
||||||
* @param string $method method of PDOStatement to be called
|
* @param string $method method of PDOStatement to be called
|
||||||
@@ -813,7 +792,7 @@ class Command extends Component
|
|||||||
Yii::info($rawSql, 'yii\db\Command::query');
|
Yii::info($rawSql, 'yii\db\Command::query');
|
||||||
|
|
||||||
if ($method !== '') {
|
if ($method !== '') {
|
||||||
$info = $this->getQueryCacheInfo();
|
$info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency);
|
||||||
if (is_array($info)) {
|
if (is_array($info)) {
|
||||||
/* @var $cache \yii\caching\Cache */
|
/* @var $cache \yii\caching\Cache */
|
||||||
$cache = $info[0];
|
$cache = $info[0];
|
||||||
|
|||||||
@@ -465,22 +465,38 @@ class Connection extends Component
|
|||||||
/**
|
/**
|
||||||
* Returns the current query cache information.
|
* Returns the current query cache information.
|
||||||
* This method is used internally by [[Command]].
|
* 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.
|
* @return array the current query cache information, or null if query cache is not enabled.
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public function getQueryCacheInfo()
|
public function getQueryCacheInfo($duration, $dependency)
|
||||||
{
|
{
|
||||||
|
if (!$this->enableQueryCache) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$info = end($this->_queryCacheInfo);
|
$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) {
|
if (is_string($this->queryCache) && Yii::$app) {
|
||||||
$cache = Yii::$app->get($this->queryCache, false);
|
$cache = Yii::$app->get($this->queryCache, false);
|
||||||
} else {
|
} else {
|
||||||
$cache = $this->queryCache;
|
$cache = $this->queryCache;
|
||||||
}
|
}
|
||||||
if ($cache instanceof Cache) {
|
if ($cache instanceof Cache) {
|
||||||
return is_array($info) ? [$cache, $info[0], $info[1]] : [$cache, null, null];
|
return [$cache, $duration, $dependency];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user