简体   繁体   English

内容更新中的缓存无效

[英]Cache invalidation on content update

i understand basic concept of cache , 我了解缓存的基本概念,

but one thing i cant able to understand , even i saw some example , 但我看不懂的一件事,即使我看到了一些例子,

assume , 假设,

my first request is 9am , now new update information coming to cache , then another same content request from other user , now system get content from cache file instead of DB, 我的第一个请求是上午9点,现在有新的更新信息要缓存,然后来自其他用户的另一个相同的内容请求,现在系统从缓存文件而不是DB获取内容,

example 1 : i set the cache expire for 1hour , 示例1:我将缓存设置为1小时,

Now time is 9am , 现在是上午9点,

9am : First request : now read the content DB and store into cache file, 9.15AM: second request : now system retrive content from cache instead of retrieve from DB, 9.24am: Few contents are modified in the DB, 上午9点:第一个请求:现在读取内容数据库并将其存储到缓存文件中,9.15上午:第二个请求:现在系统从缓存中检索内容,而不是从数据库中检索内容,上午9.24:在数据库中很少修改内容,

9.30am: Third request : NOW system retrieve the content from DB or Cache , How system know DB is updated , 9.30am:第三个请求:NOW系统现在从DB或Cache检索内容,系统如何知道DB已更新,

This is my doubt : 这是我的疑问:

Example 2: If am not set the expiry time :, 示例2:如果未设置到期时间:,

Then When System retrieve and store the new updated content from the Database to cache file. 然后,当系统从数据库检索并存储新的更新内容并将其存储到高速缓存文件时。

Simple: whenever you update a record in the database, you delete any cached copy of it. 简单:只要更新数据库中的记录,就删除它的任何缓存副本。 This forces the cache to be updated the next time the record is requested. 这将强制在下次请求记录时更新缓存。

It should work like this: 它应该像这样工作:

$data = retrieveData($id);

retrieveData() does this: retrieveData()这样做:

  • Is the data for $id in the cache? $id的数据是否在缓存中? Good, return it. 好,退货。
  • If it isn't, fetch the data from the database, write a copy to the cache and return it. 如果不是,请从数据库中获取数据,将副本写入缓存并返回。

When updating data: 更新数据时:

updateData($data);
  • updateData() saves the new $data to the database. updateData()将新的$data保存到数据库。
  • It deletes any copy of $data in the cache if there is one. 如果存在,它将删除高速缓存中$data任何副本。

This means that: 这意味着:

  • The first time you retrieve a record from the database there's no cache. 第一次从数据库检索记录时,没有缓存。 Once you have retrieved it though it'll be cached. 一旦检索到它,尽管它将被缓存。
  • The next time the same record can be taken from the cache. 下次可以从缓存中获取相同的记录。
  • When the record is updated, the cache is deleted. 记录更新时,将删除缓存。
  • The next time the record is requested, there's no cache, so it'll be retrieved from the database and the cache updated again. 下次请求记录时,没有缓存,因此将从数据库中检索记录,并再次更新缓存。
  • Rinse, repeat. 冲洗,重复。

There are two possible interpretations of what you are saying - one is to do with the database keeping results in memory which is hardly caching. 您所说的话有两种可能的解释-一种与数据库有关,结果保存在几乎不缓存的内存中。

On the other hand there is HTTP caching and by the looks of it, (assuming you are talking about HTTP caching) you have got it quite wrong - with HTTP caching user1 requests a page that expires in 3 hours, any time he requests that page within the next 3 hours no hits come to your server. 另一方面,有HTTP缓存,从外观上看(假设您在谈论HTTP缓存),您就完全错了-使用HTTP缓存,user1请求一个页面,该页面在3小时内到期,只要请求该页面在接下来的3个小时内,您的服务器没有任何点击。 This is (obviously) separate for each user - when user2 comes he has no idea who user1 is or what he has in his cache so user2 requests the data from your server, after which future requests from user2 can be served from user2's cache. (对于每个用户)这显然是分开的-当user2出现时,他不知道user1是谁或他的缓存中有什么,因此user2向您的服务器请求数据,此后可以从user2的缓存中满足user2的将来请求。

HTTP also has a method for caching data of unknown lifetime - every time a user requests a page they add an If-Modified-Since: <Date last fetched/modified> or If-None-Match: <server-specified hash> header. HTTP还具有一种用于缓存未知生命周期数据的方法-每次用户请求页面时,他们都会添加If-Modified-Since: <Date last fetched/modified>If-None-Match: <server-specified hash>标头。 The server can then send a 304 Not Modified HTTP Status Code if the content has not changed and not have to send the body of the file again. 如果内容没有更改,并且服务器不必再次发送文件正文,则服务器可以发送304 Not Modified HTTP状态代码。

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

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