[Home] [My Scripts] [Contact] [Business Card Designer] [Donate] [$8.95 Domain Names]

Home > Scripts > morecache


... feel free to use this script for whatever you like.


morecache is an output caching class. Use it to give your pages a big boost of speed.



This script uses the flock function. This will not work on Windows 9x, so you need to comment out the define() at the top of the morecache file, eg:


// define("WITH_FLOCK", 1);

class morecache{




morecache allows you to easily cache parts or all of your script's output. This is useful if you have slow database queries on data that is viewed more often than it is changed.

morecache works by using output buffering to capture the output when it is first generated, and storing it in files from which it is retrieved afterwards.


The Constructor
= new morecache("/path/tostore/cachedfiles/", 60*60);

The first argument to the constructor is where to store the cache files. Make sure this folder is writeable by your php script.

The second argument is the default time after which cache expires. You can set this value to 0 to never cache anything, or to -1 to never expire.

Caching Output

To cache something with morecache, you need to surround the code whose output you want to cache with an if() block, with a call to the cache() function as below:
= new morecache("../cachefiles", 60*60*24);

$uniqueid = "cacheexample";

$cache->cache($uniqueid, 60) ){
$res = mysql_query("SELECT lots, of, rows FROM a_very_big_table");
   while( list(
$lots, $of, $rows) = mysql_fetch_result($res)){
"$lots $of $rows<br>";

The example above checks to see if a cachefile called "cacheexample" exists, and if it does, whether it was created more than 60 seconds ago.

If it is older than this, or does not exist, the cache() function returns true, and the code that creates the output executes. The stop() function is called after the code - this function handles getting the output and storing it in a cache file for later retrieval.

If the second argument to the cache() function is omitted, eg:
if( $cache->cache($uniqueid) ){
// db query and output

then the expiry time specified in the constructor is used.

Replacing values in a cached file:

If you need to include unique information within the cached output (for example a session id string), the cache() function can take two additional arguments, an array of strings to search for, and an array of strings to replace them with.

For example, to cache the output of a query that lists links to other sections of a website, but display a unique session id in each url:


if( $cache->cache("searchreplacecache", -1, array("{SESSION}"), array(session_id()) ){
$result = mysql_query("SELECT title, url FROM links");
   while( list(
$title, $url) = mysql_fetch_row($result) ){
"<a href='$url?PHPSESSID={SESSION}'>$title</a><br>";


The above query would only be performed once, as it has been set to never expire, but each visitor
to the page will get the links outputted with their session id in place.

Removing a cache file

Sometimes, you may want to delete a cached file before its normal expiry time. An example of this is the guestbook on the contact page of this site. If visitors are just reading the messages, then the file can be retrieved from cache, but if someone posts a new message, the output needs to be refreshed.

The expirecache($uniqueid) function accomplishes this. Eg:
function postmessage($subject, $name, $message){

// add new message to database here



The $uniqueid argument ("messages" in this example), should be the same as that used to create the cache in the first place. All the expirecache() function actually does is delete the file with that name, so that next time the cache() function is called with that id, it has to regenerate the cache with the new data.



$cache = new morecache($cachepath = "", $defaultexpiry = 60)
$cachepath is where to store the cached output
$defaultexpiry is the default number of seconds that the cached output is used. Set it to -1 to be used forever.

Start caching:

$cache->cache($cacheid, $expirytime = "", $search = array(), $replace = array())
$cacheid is the unique identifier for this output.
$expirytime is when it will expire. Set to -1 to never expire, or 0 to expire now.
$search is an optional array of strings to replace in the cached output.
$replace is an optional array of strings to replace $search with.

This function returns true if the cache needs to be recreated, and false otherwise.

Stop caching:

Required when the cache() function returns true, call this function at the end of the block of code you are caching to stop caching and store the output.

Delete a cache:

$cacheid is the unique identifier for a cache

This function just deletes the cache file if it exists.


morecache.php (3950 bytes) View Download
Viewed 2425 times. Downloaded 4095 times.

[Back] [Top] [Forward]

Powered by php, mySql.

Request processed in 0.01 seconds on Wednesday 26th September 2012 23:42:36.

©2002 Sam Yapp

What do you think of my resume wizard site? All nicely written in php :)