Jack
Jack

Reputation: 9784

Will caching be appropriate for this scenario?

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!

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

Upvotes: 0

Views: 116

Answers (3)

bschaeffer
bschaeffer

Reputation: 2904

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

Upvotes: 1

mhitza
mhitza

Reputation: 5715

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.

Upvotes: 0

timdev
timdev

Reputation: 62894

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.

Upvotes: 6

Related Questions