cache() public method
Uses query cache for the queries performed with the callable.
When query caching is enabled ($enableQueryCache is true and $queryCache refers to a valid cache), queries performed within the callable will be cached and their results will be fetched from cache if available. For example,
// The customer will be fetched from cache if available. // If not, the query will be made against DB and cached for use next time. $customer = $db->cache(function (Connection $db) { return $db->createCommand('SELECT * FROM customer WHERE id=1')->queryOne(); });
Note that query cache is only meaningful for queries that return results. For queries performed with yii\db\Command::execute(), query cache will not be used.
See also:
public mixed cache ( callable $callable, $duration = null, $dependency = null ) | ||
---|---|---|
$callable | callable |
A PHP callable that contains DB queries which will make use of query cache. The signature of the callable is |
$duration | integer |
The number of seconds that query results can remain valid in the cache. If this is not set, the value of $queryCacheDuration will be used instead. Use 0 to indicate that the cached data will never expire. |
$dependency | yii\caching\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 |
Please login to continue.