The Way of the Cache

The Way of the Cache.

There is a post at the Mambo forums about benchmarking different PHP content management systems. The winners were Drupal and e107 because they support HTML caching.

This is clear evidence that so long as it is practical to cache the rendered HTML, the speed of your PHP code is irrelevant.
In response the these results, some of the Mambo developers implemented a caching hack:

I reran the test with the latest CVS (i.e. with caching hacked in)…

Processor load halved (peak of .84 instead of 1.4, slowest page time
halved (.36 from .74) and average page time was .16 instead of .26 so
down by a little over a third. Nice results for a quick hack Robert

It's not that difficult to implement a caching scheme for your web application. You only need:

  • A caching library. I like Cache_Lite if you want to store the HTML in a file. Or you can cache the HTML in a database; Drupal does this.

  • Find all commonly rendered HTML that does not require
    personalization, eg. articles, navigation toolbars, etc, and customize
    the relevant display code to cache the HTML. It helps if you have a
    simple UI, with the ability to render by area or block (eg. Left
    Column, Right Column, Main Article Area).

  • For each area to be cached, determine a hashing algorithm for the item you want to cache. A simple one would be md5($HTML_BLOCK_NAME.$article_id). If you can cache the whole page (e.g. no user has logged in), even better.

  • Decide on the cache invalidation events, such as “on procreate”, “on mutilate”, “on dismember” and “on timeout”.

Thanks to
PHP Kitchen for the link. 
[PHP Everywhere – Thoughts on PHP and other technologies by John Lim]

Leave a comment