diff --git a/framework/db/Command.php b/framework/db/Command.php index e098a92016..1287558bce 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -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. * @param string $method method of PDOStatement to be called @@ -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]; diff --git a/framework/db/Connection.php b/framework/db/Connection.php index a11e39aabb..a756b109dc 100644 --- a/framework/db/Connection.php +++ b/framework/db/Connection.php @@ -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; }