Making PHP Applications Cache-Friendly
The basic approach I am using here is to touch a zero-length file
whenever the database is updated. The file’s modification time will
then serve as the Last-Modified date. This is very simple to
implement, but rather primitive as it does not differentiate
between forums: if something is posted in forum X, all
forums in the same database are considered “updated”. That’s
clearly not very effective when you have lots of posting activity
in more than one or two forums.
whenever the database is updated. The file’s modification time will
then serve as the Last-Modified date. This is very simple to
implement, but rather primitive as it does not differentiate
between forums: if something is posted in forum X, all
forums in the same database are considered “updated”. That’s
clearly not very effective when you have lots of posting activity
in more than one or two forums.
A finer-grained scheme — perhaps
down to tracking individual threads — would be more appropriate in
that case. Additionally, there are some issues with Phorum’s use
of cookies to flag unread messages; the workaround used here is
to allow a full reload periodically.
down to tracking individual threads — would be more appropriate in
that case. Additionally, there are some issues with Phorum’s use
of cookies to flag unread messages; the workaround used here is
to allow a full reload periodically.
Please note that the following code is not release quality.
It works, but it’s rather simplistic and should be considered a
“proof of concept”. I simply patched Phorum
3.2.11 code where it seemed necessary to get quick results. My
changes are in bold print.
It works, but it’s rather simplistic and should be considered a
“proof of concept”. I simply patched Phorum
3.2.11 code where it seemed necessary to get quick results. My
changes are in bold print.
common.php:
<?php
if ( !defined( "_COMMON_PHP" ) ){
define("_COMMON_PHP", 1 );
// These variables may be altered as needed:
$modification_file = '/var/tmp/.phorum-update';
// table name that Phorum uses to access meta-information on forums.
?>
include/header.php:
<?php
global $modification_file;
if(isset($modification_file) && ($mtime = filemtime($modification_file))) {
// to be on the safe side, refresh last modification time
// if it's "too old", forcing a reload from time to time.
// this is probably not necessary for most applications
if((mktime() - $mtime) > 3600) {
touch($modification_file);
$mtime = mktime();
}
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
// we assume that the client generates proper RFC 822/1123 dates
// (should work for all modern browsers and proxy caches)
if ($HTTP_IF_MODIFIED_SINCE == $gmt_mtime) {
header("HTTP/1.1 304 Not Modified");
exit;
}
$lastmod_header = "Last-Modified: " . $gmt_mtime;
Header($lastmod_header);
}
// your header modifications go here...
?>