Chaching - tutoria, guidelines, real-life examples


I have little experience with caching. But I will be involved in a larger project and I need to get used to it.

Please - how to learn? What data need caching? What are strategies? Where to use caching? Should I cache all database queries for instance or only some special ones? How to properly use database query caching with related records (real life examples, not just theory!).

What kind of caching medium to use? What is Redis good for? Where to use fragment caching and where to use page caching?

This is so little covered in the context of Yii, that I feel really lost.

Thanks ahead!

Really - NOBODY ???

Sorry, Ziggi. I’m not sure about others but for me this is such a broad topic that I feel answering to it with 2-3 sentences is not enough and at the same time I’m not that competent to write the full article.

General idea is to lower the cost of all resources when fetching the data and by resources I mean time, CPU, data transfer etc. If cost of fetching cached data < cost od fetching not cached data - go for cache. And this applies for any kind of cache.

As for the specific problems you mentioned I’m afraid you have to google it. There are plenty of articles about caching and different caching mechanisms.

If there is something not clear about using cache with Yii don’t hesitate to ask here.


There is a well knows sentence by a renown programmer Phil Karlton:

Consequently - while I am capable of thinking how to invalidate an outdated webpage or even it’s particular fragment - how actually I invalidate outdated cached database queries? In case of a related records - any inconsistency can lead to a total disaster!

Best regards!

The way you invalidating cache depends very much on how bad it is to get outdated data. If you’re simply caching weather for the front page of your website it doesn’t matter if it will be outdated for an hour so simple time-based invalidation is OK. On the other hand, if you absolutely need to be sure data is always valid, either not cache at all or reset cache on every update that may affect the data you’re caching. That’s usually done by tagging and isn’t simple at all since you need to manually invalidate cache.

Thank you, Samdark!


  1. What Yii provides is sufficient to implement any caching you like.

  2. Caching strategy is up to you. That’s tricky part no framework could magically solve.