From a036fac490bde311669ba6e26518e33c0f00be00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=80=8D=E7=8E=AE?= Date: Sat, 10 Feb 2018 18:30:58 +0800 Subject: [PATCH] Extract cache from `yii\db\Command` to `yii\db\CacheableQueryTrait` and use it in `yii\db\Query` --- framework/db/ActiveQuery.php | 15 +++++-- framework/db/CacheableQueryTrait.php | 63 ++++++++++++++++++++++++++++ framework/db/Command.php | 39 +---------------- framework/db/Query.php | 16 +++++-- 4 files changed, 88 insertions(+), 45 deletions(-) create mode 100644 framework/db/CacheableQueryTrait.php diff --git a/framework/db/ActiveQuery.php b/framework/db/ActiveQuery.php index 7253d86192..7dd264b395 100644 --- a/framework/db/ActiveQuery.php +++ b/framework/db/ActiveQuery.php @@ -319,7 +319,11 @@ class ActiveQuery extends Query implements ActiveQueryInterface $params = $this->params; } - return $db->createCommand($sql, $params); + $command = $db->createCommand($sql, $params); + if ($this->hasCache()) { + $command->cache($this->queryCacheDuration, $this->queryCacheDependency); + } + return $command; } /** @@ -337,11 +341,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface return parent::queryScalar($selectExpression, $db); } - return (new Query())->select([$selectExpression]) + $command = (new Query())->select([$selectExpression]) ->from(['c' => "({$this->sql})"]) ->params($this->params) - ->createCommand($db) - ->queryScalar(); + ->createCommand($db); + if ($this->hasCache()) { + $command->cache($this->queryCacheDuration, $this->queryCacheDependency); + } + return $command->queryScalar(); } /** diff --git a/framework/db/CacheableQueryTrait.php b/framework/db/CacheableQueryTrait.php new file mode 100644 index 0000000000..52d0cdf08a --- /dev/null +++ b/framework/db/CacheableQueryTrait.php @@ -0,0 +1,63 @@ + + * @author Dmytro Naumenko + * @since 2.0.14 + */ +trait CacheableQueryTrait +{ + /** + * @var int the default number of seconds that query results can remain valid in cache. + * Use 0 to indicate that the cached data will never expire. And use a negative number to indicate + * query cache should not be used. + * @see cache() + */ + public $queryCacheDuration; + /** + * @var \yii\caching\Dependency the dependency to be associated with the cached query result for this command + * @see cache() + */ + public $queryCacheDependency; + + /** + * Enables query cache for this command or query. + * @param int $duration the number of seconds that query result of this command or query can remain valid in the cache. + * If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead. + * Use 0 to indicate that the cached data will never expire. + * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached result. + * @return $this the command or query object itself + */ + public function cache($duration = null, $dependency = null) + { + $this->queryCacheDuration = $duration !== null ? $duration : $this->db->queryCacheDuration; + $this->queryCacheDependency = $dependency; + return $this; + } + + /** + * Disables query cache for this command or query. + * @return $this the command or query object itself + */ + public function noCache() + { + $this->queryCacheDuration = -1; + return $this; + } + + /** + * Checks, whether caching is enabled + * @return bool + */ + public function hasCache() + { + return $this->queryCacheDuration !== null || $this->queryCacheDependency !== null; + } +} diff --git a/framework/db/Command.php b/framework/db/Command.php index f775392f1c..7320d4d682 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -56,6 +56,8 @@ use yii\base\NotSupportedException; */ class Command extends Component { + use CacheableQueryTrait; + /** * @var Connection the DB connection that this command is associated with */ @@ -75,18 +77,6 @@ class Command extends Component * and is used to generate [[rawSql]]. Do not modify it directly. */ public $params = []; - /** - * @var int the default number of seconds that query results can remain valid in cache. - * Use 0 to indicate that the cached data will never expire. And use a negative number to indicate - * query cache should not be used. - * @see cache() - */ - public $queryCacheDuration; - /** - * @var \yii\caching\Dependency the dependency to be associated with the cached query result for this command - * @see cache() - */ - public $queryCacheDependency; /** * @var array pending parameters to be bound to the current PDO statement. @@ -112,31 +102,6 @@ class Command extends Component private $_retryHandler; - /** - * Enables query cache for this command. - * @param int $duration the number of seconds that query result of this command can remain valid in the cache. - * If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead. - * Use 0 to indicate that the cached data will never expire. - * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query result. - * @return $this the command object itself - */ - public function cache($duration = null, $dependency = null) - { - $this->queryCacheDuration = $duration === null ? $this->db->queryCacheDuration : $duration; - $this->queryCacheDependency = $dependency; - return $this; - } - - /** - * Disables query cache for this command. - * @return $this the command object itself - */ - public function noCache() - { - $this->queryCacheDuration = -1; - return $this; - } - /** * Returns the SQL statement for this command. * @return string the SQL statement to be executed diff --git a/framework/db/Query.php b/framework/db/Query.php index 8539ddb43d..2d17f54da3 100644 --- a/framework/db/Query.php +++ b/framework/db/Query.php @@ -50,6 +50,7 @@ use yii\base\InvalidParamException; class Query extends Component implements QueryInterface { use QueryTrait; + use CacheableQueryTrait; /** * @var array the columns being selected. For example, `['id', 'name']`. @@ -129,7 +130,11 @@ class Query extends Component implements QueryInterface } list($sql, $params) = $db->getQueryBuilder()->build($this); - return $db->createCommand($sql, $params); + $command = $db->createCommand($sql, $params); + if ($this->hasCache()) { + $command->cache($this->queryCacheDuration, $this->queryCacheDependency); + } + return $command; } /** @@ -449,11 +454,14 @@ class Query extends Component implements QueryInterface return $command->queryScalar(); } - return (new self()) + $command = (new self()) ->select([$selectExpression]) ->from(['c' => $this]) - ->createCommand($db) - ->queryScalar(); + ->createCommand($db); + if ($this->hasCache()) { + $command->cache($this->queryCacheDuration, $this->queryCacheDependency); + } + return $command->queryScalar(); } /**