简体   繁体   中英

Query cache in yii

If I want to use Query Caching as in the tutorial here :

Yii Query Caching Guide

whether I should have a update_time field in every table?

$dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl_post');

Is the field must exist to be able to use the query cache?

The guide only gives you an example, how you can use a cache dependency with query caching, but a dependency is not mandatory. If you use one, you can come up with your own SQL to find out, if the cached content is still valid. So the update_time field is also not mandatory.

to answer your question, as Michael has already written, it's not mandatory. To Know more about this, You should understand the role played by the dependency query.

Consider an example. You want to cache the query which will list all the users. Something like SELECT * FROM users . Assume that at any point of time, it is 100 users.

Now the main question is when do you think your cached data becomes invalid? Whenever the new user is added, you need to re-query (Not to use Cached Data/Invalidate Cache Data). How to find that new user is added? By Querying the ID Number. So if the query SELECT MAX(ID) FROM Users returns different value then it means the new user was added.

Remember If the user is deleted, then my example will not be valid. That time, after deleting the user, you should delete the Respective Cache Data.

In this way, you should find out the relevant Queries which can be used to Validate the Cache Data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM