Below is a simple example of php caching. It can dramatically reduce the load on the server by bypassing mysql, or other database pulls, for relatively static content.
< ?php
$cachefile = 'cache/cachedData.xml';
$cachetime = 1 * 60 * 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
header('Content-type: application/atom+xml; charset=UTF-8');
include($cachefile);
// echo "\n";
exit;
}
ob_start(); // Start the output buffer
// create content here
// if the page is not already cached and loaded from above it will be created here and delivered
// The content will then be cached below for faster future accessing
// this works well for information that changes slowly or not at all.
// Cache the output to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
?>
No comments:
Post a Comment