Commit 27132ced by Qiang Xue

Fixes #4384.

parent 98a86ce7
......@@ -267,7 +267,7 @@ Query caching can be used for [DAO](db-dao.md) as well as [ActiveRecord](db-acti
Query caching has three global configurable options through [[yii\db\Connection]]:
* [[yii\db\Connection::enableQueryCache|enableQueryCache]]: whether to turn on or off query caching.
It defaults to false. Note that to effectively turn on query caching, you also need to have a valid
It defaults to true. Note that to effectively turn on query caching, you also need to have a valid
cache, as specified by [[yii\db\Connection::queryCache|queryCache]].
* [[yii\db\Connection::queryCacheDuration|queryCacheDuration]]: this represents the number of seconds
that a query result can remain valid in the cache. You can use 0 to indicate a query result should
......
......@@ -852,7 +852,6 @@ class Command extends Component
Yii::endProfile($token, 'yii\db\Command::query');
} catch (\Exception $e) {
Yii::endProfile($token, 'yii\db\Command::query');
$this->db->resetQueryCacheInfo();
throw $this->db->getSchema()->convertException($e, $rawSql);
}
......
......@@ -204,11 +204,12 @@ class Connection extends Component
* @var boolean whether to enable query caching.
* Note that in order to enable query caching, a valid cache component as specified
* by [[queryCache]] must be enabled and [[enableQueryCache]] must be set true.
* Also, only the results of the queries enclosed within [[cache()]] will be cached.
* @see queryCache
* @see cache()
* @see noCache()
*/
public $enableQueryCache = false;
public $enableQueryCache = true;
/**
* @var integer the default number of seconds that query results can remain valid in cache.
* Use 0 to indicate that the cached data will never expire.
......@@ -399,6 +400,7 @@ class Connection extends Component
* Use 0 to indicate that the cached data will never expire.
* @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query results.
* @return mixed the return result of the callable
* @throws \Exception if there is any exception during query
* @see enableQueryCache
* @see queryCache
* @see noCache()
......@@ -406,9 +408,14 @@ class Connection extends Component
public function cache(callable $callable, $duration = null, $dependency = null)
{
$this->_queryCacheInfo[] = [$duration === null ? $this->queryCacheDuration : $duration, $dependency];
$result = call_user_func($callable, $this);
array_pop($this->_queryCacheInfo);
return $result;
try {
$result = call_user_func($callable, $this);
array_pop($this->_queryCacheInfo);
return $result;
} catch (\Exception $e) {
array_pop($this->_queryCacheInfo);
throw $e;
}
}
/**
......@@ -430,6 +437,7 @@ class Connection extends Component
* @param callable $callable a PHP callable that contains DB queries which should not use query cache.
* The signature of the callable is `function (Connection $db)`.
* @return mixed the return result of the callable
* @throws \Exception if there is any exception during query
* @see enableQueryCache
* @see queryCache
* @see cache()
......@@ -437,9 +445,14 @@ class Connection extends Component
public function noCache(callable $callable)
{
$this->_queryCacheInfo[] = false;
$result = call_user_func($callable, $this);
array_pop($this->_queryCacheInfo);
return $result;
try {
$result = call_user_func($callable, $this);
array_pop($this->_queryCacheInfo);
return $result;
} catch (\Exception $e) {
array_pop($this->_queryCacheInfo);
throw $e;
}
}
/**
......@@ -461,16 +474,6 @@ class Connection extends Component
}
/**
* Cleans up the query cache information.
* This method is used internally by [[Command]].
* @internal
*/
public function resetQueryCacheInfo()
{
$this->_queryCacheInfo = [];
}
/**
* Establishes a DB connection.
* It does nothing if a DB connection has already been established.
* @throws Exception if connection fails
......
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