Use Cache On Eager Or Lazy Load Of Models


Is there a way to load related model object (eager or lazy) with cache ?

for example:

$category = Category::model()->findByPk(1);

$products = $category->products //(there is a relation type HAS_MANY)

so, how to achieve something like that ?

$products = $category->cache()->products (as Yii::app()->db->cache()->createCommand(...)) ?


You risk putting a lot of data in your cache. Also, depending on which type of cache you’re using, you may be deserialising a large amount of data on each request, which might well be slower and more processor intensive than just fetching the items from the database each time. Bear in mind that databases have facilities for optimising common queries.

Have you identified a bottleneck in your application?

Hi Keith,

Thanks for your suggestion.

Even if there is a way to do it through the cache, the problem will remain. So you are right…

I had override the afterFind (category) to load extra data from related table (products id) using CActiveRecord I will replace my code with a sql command query (DAO) to improve the performance