cache.php
<?php
function cache_start()
{
global $cache_file_name, $age;
// a superbly creative way for creating cache files
$cache_file_name = __FILE__ . '_cache';
// default cache age
if (empty($age)) $age = 600;
// check if cache exists and if the cached data is still valid
if (@filemtime($cache_file_name) + $age > time()) {
// Yey! cache hit, output cached data and exit
readfile($cache_file_name);
unset($cache_file_name);
exit;
}
// nothing in cache or cache is too old
ob_start();
}
function cache_end()
{
global $cache_file_name;
// nothing to do
if (empty($cache_file_name)) return;
// fetch output of the script
$str = ob_get_clean();
// output data to the user, so they don't need to wait
// for the cache writing to complete
echo $str;
// write to cache
fwrite(fopen($cache_file_name.'_tmp', "w"), $str);
// atomic write
rename($cache_file_name.'_tmp', $cache_file_name);
}
cache_start();
// set cache termination code as the exit handler
// this way we don't need to modify the script
register_shutdown_function("cache_end");
?>
index.php
<?php
require "./cache.php"; // our cache code
// Simple guestbook script.
$db = new sqlite_db("gb.sqlite");
$r = $db->array_query("SELECT * FROM guestbook", SQLITE_ASSOC);
foreach ($r as $row) {
echo $r->user . ' wrote on ' . date("Ymd", $r->date) . ":<br />\n";
echo $r->message . "<hr /><hr />";
}
?>
Source:
Performance Tuning PHP
http://talks.php.net/show/perf_tunning/20
Title:
Content caching in PHP
Description:
cache.php <?php function cache_start() { global $cache_file_name, $age; // a superbly creative way for creating cache files $...
...
Rating:
4