PHP Website Cache Script

by Hiroshi on February 3, 2013

in cache

PHP website cache system will reduce server load, make website more responsive and fast. It will reduce database requests and save time that sever takes to translate PHP pages before sending them to browser. This cache script converts PHP pages in static pages and stores them in cache folder. If static version of page exists in cache folder it will be served unless PHP page will be shown and its cache will be created to show next time visit. Here is simple cache script ready to use and implement in any PHP based dynamic website.

I am using folder named as “siteCache” at the website root directory to store this cache, so make one.

<?php
// cache - will work online - might not work on localhost
// location (folder name) and prefix for cache files
define('CACHE_PATH', "siteCache/");
// how long to keep the cache files (time in hours)
define('CACHE_TIME', 12);
// return location and name for cache file
function cache_file()
{
return CACHE_PATH . md5($_SERVER['REQUEST_URI']);
}
// display cached file if present and not expired
function cache_display()
{
$file = cache_file();
// check that cache file exists and is not too old
if(!file_exists($file)) return;
if(filemtime($file) < time() - CACHE_TIME * 3600) return;
// if so, display cache file and stop processing
readfile($file);
exit;
}
// write to cache file
function cache_page($content)
{
if(false !== ($f = @fopen(cache_file(), 'w')))
{
fwrite($f, $content);
fclose($f);
}
return $content;
}
// execution stops here if valid cache file found
cache_display();
// enable output buffering and create cache file
ob_start('cache_page');
?>

That’s it.

How to Implement Website Cache

Create a PHP file, copy above code in that. PHP Include (call) that file on top of every page. You can Include cache code file in site pages Like below:

<?php session_start();
include("db-file.php"); 
include("cache-file.php"); ?>

Session start code always comes at top of page and then the db or configuration file and then you can include cache file. Or – Copy cache script code in database connection file or configuration file that is being called in every page of website.

Problem with this Cache Code

It will cache site pages and make your dynamic website run faster by creating static pages resulting in reduction in database queries, database load and reduction in server time consumption for php page translation to browser.

But it will create whole PHP page static and if that static page is present in cache folder, static page will be shown always unless its deleted. So what about Login forms, sessions stored data and get/post variables to pages?

If user logs in and return to index page of welcome page, he will see old static page that’s being fetched by the cache. There is a work around / fix for that.

Workaround / Fix

Every time user submits form, stores data in session or passes any variable through form to another page – delete all cache from cache folder by using this script.

Related Posts

Previous post:

Next post: