framework/db/Connection.php
public function cache(callable $callable, $duration = null, $dependency = null)
    {
        $this->_queryCacheInfo[] = [$duration === null ? $this->queryCacheDuration : $duration, $dependency];
        try {
            $result = call_user_func($callable, $this);
            array_pop($this->_queryCacheInfo);
            return $result;
        } catch (\Exception $e) {
            array_pop($this->_queryCacheInfo);
            throw $e;
        } catch (\Throwable $e) {
            array_pop($this->_queryCacheInfo);
            throw $e;
        }
    }
public function cache(callable $callable, $duration = null, $dependency = null)
    {
        $this->_queryCacheInfo[] = [$duration === null ? $this->queryCacheDuration : $duration, $dependency];
        try {
            $result = call_user_func($callable, $this);
            array_pop($this->_queryCacheInfo);
            return $result;
        } catch (\Exception $e) {
            array_pop($this->_queryCacheInfo);
            throw $e;
        } catch (\Throwable $e) {
            array_pop($this->_queryCacheInfo);
            throw $e;
        }
    }
public function cache ( callable $ callable , $ duration = null , $ dependency = null ) { $ this -> _queryCacheInfo [ ] = [ $ duration === null ? $ this -> queryCacheDuration : $ duration , $ dependency ] ; try { $ result = call_user_func ( $ callable , $ this ) ; array_pop ( $ this -> _queryCacheInfo ) ; return $ result ; } catch ( \ Exception $ e ) { array_pop ( $ this -> _queryCacheInfo ) ; throw $ e ; } catch ( \ Throwable $ e ) { array_pop ( $ this -> _queryCacheInfo ) ; throw $ e ; } }
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,

```php
// 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
[[Command::execute()]], query cache will not be used.

@param callable $callable a PHP callable that contains DB queries which will make use of query cache.
The signature of the callable is `function (Connection $db)`.
@param int $duration 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.
@param \yii\caching\Dependency $dependency the cache dependency associated with the cached query results.
@return mixed the return result of the callable
@throws \Exception|\Throwable if there is any exception during query
@see enableQueryCache
@see queryCache
@see noCache()
Uses query cache for the queries performed with the callable .