开发者

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,

example 1 : i set the cache expire for 1hour ,

Now time is 9am ,

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: 开发者_如何学JAVAFew contents are modified in the DB,

9.30am: Third request : NOW system retrieve the content from DB or Cache , How system know DB is updated ,

This is my doubt :

Example 2: If am not set the expiry time :,

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:

  • Is the data for $id in the cache? 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.
  • It deletes any copy of $data in the cache if there is one.

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. 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.

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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜