How to do query caching in Active Record models ?

I am quoting the guide:

Query caching is a special caching feature built on top of data caching. It is provided to cache the result of database queries.

Query caching requires a DB connection and a valid cache application component. The basic usage of query caching is as follows, assuming $db is a yii\db\Connection instance:


$result = $db->cache(function ($db) {


    // the result of the SQL query will be served from the cache

    // if query caching is enabled and the query result is found in the cache

    return $db->createCommand('SELECT * FROM customer WHERE id=1')->queryOne();


});

I do not think that I will manually create db connection in AR classes. So how to do this in my AR models ?

Cache() returns the same as callback:


$result = $db->cache(function ($db) {


    // the result of the SQL query will be served from the cache

    // if query caching is enabled and the query result is found in the cache

    return MyModel::find()->asArray()->all();


});

Thanks,

but where should this come from in my AR model ? :


$db->cache(function ($db)

Do I have to create $db instance manually in AR model ? I am confused.

Anyone ?

The database component, obviously :)

But do I have to instantiate it manually like when I am using DAO, since I am in AR ?

Second paragraph here:

http://www.yiiframework.com/doc-2.0/guide-db-dao.html

Gotta love the Yii docs :)

WoW…thanks :)