views:

52

answers:

3

Hi folks,

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

Please bear 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

+6  A: 

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.

timdev
Thanks @timdev, this really helped. So it turns out CodeIgniter's cache system *doesn't* include a cache invalidation method, however, it looks like some users have created their own hook on the wiki: http://codeigniter.com/wiki/Clear_Page_Cache/
Jack Webb-Heller
Indeed. I meant to update my answer. Page-level caching is often not very helpful. You might look into lower-level kinds of caching. For instance, mySQL's query cache is a pretty easy thing to set up, and is very helpful in some cases. You can also write your own cache logic inside your models. You might look at Zend_Cache, which should play nicely with CI, and is quite nice.
timdev
A: 

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.

mhitza
A: 

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

bschaeffer