Cake’s core cache helper is great, but the files it outputs are PHP files, so it will never be as fast as straight HTML files. This HTML Cache Helper writes out pure HTML, meaning the web server doesn’t have to touch PHP when a request is made. Yea, I know there are some huge limitations with this. First of all you can’t have any user/session specific code on the page. Also there is no way to automatically check if the cache is expired and needs to be rebuilt.

Uses

I use this helper on RSStalker.com. It handles the custom RSS feeds (currently around 13k), which is perfect since there is nothing user specific in the XML. Each feed gets hit multiple times a day, by multiple aggregators. This really adds up to a ton of requests.

The Code

You can download it here. Or just copy and paste this into /app/views/helpers/html_cache.php:

<?php
/*
 * HTML Cache CakePHP Helper
 * Copyright (c) 2008 Matt Curry
 * www.PseudoCoder.com
 * http://github.com/mcurry/cakephp/tree/master/helpers/html_cache
 * http://www.pseudocoder.com/archives/2008/09/03/cakephp-html-cache-helper/
 *
 * @author      mattc <matt@pseudocoder.com>
 * @license     MIT
 *
 */

class HtmlCacheHelper extends Helper {
  function afterLayout() {
    if(Configure::read('debug') > 0) {
      return;
    }

    $view =& ClassRegistry::getObject('view');
    $path = WWW_ROOT . 'cache' . DS
            . implode(DS, array_filter(explode('/', $this->here)))
            . DS . 'index.html';

    $file = new File($path, true);
    $file->write($view->output);
  }
}
?>

There really isn’t much to it. Just add it to any controller that you want to cache the output of.

In addition you need to add two line to your webroot/.htaccess, so that the rewrite section looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}/cache/$1/index.html -f
    RewriteRule ^(.*)$ /cache/$1/index.html [L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Issues

To expire the cache I use a cron job which deletes old files from the directory.

find /full/path/to/app/webroot/cache -mmin +360 | xargs rm -f

The cached files are getting written right to your webroot. The default Cake .htaccess checks to see if a file actually exists, this is what allows images, js, css, and other files to be handled directly by the web server.

This won’t work with the root file of your controller. So for example www.rsstalker.com/feeds won’t work, but www.rsstalker.com/feeds/amazon does.

Popularity: 14% [?]