HTML Cache Helper

by mattc
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.
This helper is for sites with high traffic pages that have nothing unique about the user on the page. I use this helper on http://www.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.

Limitations

  • Nothing specific to a user on the page. No "Welcome, Matt" or shopping carts.
  • Cache will never expire. See below for workaround.


Download the helper at http://github.com/mcurry/cakephp/tree/master/helpers/html_cache or copy and paste from below to /app/views/helpers/html_cache.php.

Helper Class:

<?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-helpercakephp-html-cache-helper/
 *
 * @author      Matt Curry <matt@pseudocoder.com>
 * @license     MIT
 *
 */

class HtmlCacheHelper extends Helper {
  function 
afterLayout() {
    
$view =& ClassRegistry::getObject('view');
    
$path WWW_ROOT implode(DSarray_filter(explode('/'$this->here)));

    
$file = new File($pathtrue);
    
$file->write($view->output);
  }
}
?>

Expiring The Cache

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

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

Notes

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.

Report

More on Helpers

Tags

Advertising

Comments

  • Toss posted on 01/18/11 01:57:38 PM
    This is a fantastic plugin, thanks a lot!

    I've just had some troubles making it work with your .htaccess modification, but I've found a solution witch is even better for my needs: I renamed the ./app/webroot/index.php to index_cake.php, and created a new index.php file with the logic I needed: no cache for authenticated users, cache only some actions, etc. This is (almost) it:

    session_name('CAKEPHP');
    session_start();
    $cache = 1;

    if (!empty($_SESSION['Auth']['User']['id'])) { // if true, this is an authenticated users... no cache for him!
    $cache = 0;
    }
    ... some other logic ...

    if ($cache == 0) {
    include('index_cake.php');
    } else {
    $queryString = str_replace("url=", "", $_SERVER["QUERY_STRING"]);
    $baseUrl = str_replace($queryString, "", $_SERVER["REQUEST_URI"]);
    $cacheFile = './cache/site'.$baseUrl.$_GET['url'].'/index.html';

    if (is_readable($cacheFile)) {
    include($cacheFile);
    } else {
    include('index_cake.php');
    }
    }

    This does the job perfectly!
  • Toss posted on 01/18/11 01:56:25 PM
    This is a fantastic plugin, thanks a lot!

    I've just had some troubles making it work with your .htaccess modification, but I've found a solution witch is even better for my needs: I renamed the ./app/webroot/index.php to index_cake.php, and created a new index.php file with the logic I needed: no cache for authenticated users, cache only some actions, etc. This is (almost) it:



    This does the job perfectly!
  • mattc posted on 10/23/08 07:21:29 PM
login to post a comment.