简体   繁体   English

在yii中查询缓存

[英]Query cache in yii

If I want to use Query Caching as in the tutorial here : 如果我想在此处的教程中使用查询缓存:

Yii Query Caching Guide Yii查询缓存指南

whether I should have a update_time field in every table? 我是否应该在每个表中都有update_time字段?

$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. 如果您使用一个,如果缓存的内容仍然有效,您可以提出自己的SQL来查找。 So the update_time field is also not mandatory. 因此update_time字段也不是必需的。

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 . SELECT * FROM users这样的东西。 Assume that at any point of time, it is 100 users. 假设在任何时间点,它是100个用户。

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. 通过查询ID号。 So if the query SELECT MAX(ID) FROM Users returns different value then it means the new user was added. 因此,如果查询SELECT MAX(ID) FROM Users返回不同的值,则表示添加了新用户。

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. 通过这种方式,您应该找到可用于验证缓存数据的相关查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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