开发者

Will caching be appropriate for this scenario?

So I have a PHP CodeIgniter webapp and am trying to decide whether to incorporate caching.

Please be开发者_运维知识库ar with me on this one, since I'll happily admit I don't fully understand caching!

  • So the first user loads up a page of user submitted-content. It takes 0.8 seconds (processing) to load it 'slow'. The next user then loads up that same page, it takes 0.1 seconds to load it 'fast' from cache.

  • The third user loads it up, also taking 0.1 seconds execution time. This user decides to comment on the page.

  • The fourth user loads it up 2 minutes later but doesn't see the third user's comment, because there's still another 50 minutes left before the cache expires

What do you do in this situation? Is it worth incorporating caching on pages like this?

The reason I'd like to use caching is because I ran some tests. Without caching, my page took an average of 0.7864 seconds execution time. With caching, it took an average of 0.0138 seconds. That's an improvement of 5599%!

I understand it's still only a matter of milliseconds, but even so...

Jack


You want a better cache.

Typically, you should never reach your cache's timeout. Instead, some user-driven action will invalidate the cache.

So if you have a scenario like this:

  1. Joe loads the page for the first time (ever). There is no cache, so it takes a while, but the result is cached along the way.
  2. Mary loads the page, and it loads quickly, from the cache.
  3. Mary adds a comment. The comment is added to the database (or whatever), and the software invalidates the cache
  4. Pete comes along and loads the page, the cache is invalid, so it takes a second to render the page, and the result is cached (as a valid cache entry)
  5. Sam comes along, page loads fast
  6. Jenny comes along, page loads fast.

I'm not a CodeIgniter guy, so I'm not sure what that framework will do for you, but the above is generally what should happen. Your application should have enough smarts built-in to invalidate cache entries when data gets written that requires cache invalidation.


Try CI's query caching instead. The page is still rendered every time but the DB results are cached... and they can be deleted using native CI functionality (i.e no third party libraries).


While CI offers only page level caching, without invalidation I used to handle this issue somewhat differently. The simplest way to handle this problem was to load all the heavy content from the cache, while the comments where loaded via a non cacheable Ajax calls.

Or you might look into custom plugins which solve this, like the one you pointed out earlier.

It all comes down to the granularity at which you want to control the cache. For simple things like blogs, loading comments via external ajax calls (on demand - as in user explicitly requests the comments) is the best approach.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜