PHP - how to create a global variable store (singleton)

My problem was that I wanted to store expensive result sets in a global variable store so that hit to my web site wouldn't impact too badly on my database.
As the homepage for the site in question contained 5 different boxes with information that came from the database, any serious amount of hits on the hompepage would have brought the database to a standstill.

The main problem here is that php doesn't have an instance of itself in memory - it is invoked whenever a page is hit and then is closed down again - so you can not set global variables that are available to other invocations of php. What was needed was a store outside php to cater for this problem.

Creating a store for global variables

I chose to serialze my object (in my case, a database result set, but it can be any form of variable / hashtable that you can store this way. I created a function that is available to all my php scripts (by including the file in which the function resides into each page).

function set_global($name,$value) {
$file = $path_to_dir_that_is_writeable_for_apache.$name;
$file_handle = fopen($file,"w");
$value = serialize($value);
fwrite($file_handle,$value);
fclose($file_handle);
}

Pretty easy, this. Just pass a unique name for this object store and the object itself to the function and it will store the object on the file system. Yes, the $path_to_dir_that_is_writeable_for_apache has to be writeable for the apache process, otherwise this won't work.

Creating a function to get the global variable out of the store

function get_global($name) {
$file = $path_to_dir_that_is_writeable_for_apache.$name;
if(!file_exists($file)) {
return false;
}
$handle = fopen($file,"r");
$buffer = '';
if ($handle) {
while (!feof($handle)) {
$buffer = $buffer.fgets($handle, 4096);
}
} else {
return null;
}
fclose($handle);
return(unserialize($buffer));
}

This function returns "false" if the store for the object $name doesn't exist. If the store exists, it will retrieve the object and return it.

Creating a function that lets you reset a global variable

function unset_global($name) {
$file = $path_to_dir_that_is_writeable_for_apache.$name;
if(file_exists($file)) {
unlink($file);
}
}

This function simply deletes the file with the name of the global object. Putting it all together - using the global store

Here is an example of a php script that uses the global store to save a result set of a complex sql query to avoid having to go to the database every time the page is hit.

$sql = 'select complex stuff from my database which is very costly and which I don\'t want carried out every time the page is hit';

$results = get_global($name);
if (!$results) {
$results = db_get_all($sql);
set_global($name,$results);
}
}

The function "db_get_all" returns all results from the query to the database as a 2-dimensional hashtable. So if the retrieval of the result set from the global store fails, the result set is created from a database query and then written to global store. If it's stored in the global store, it is retrieved and we save outselves the database query.

Limitations

You have to weight the cost of retrieving the data from the database to the cost of unserializing the object from file.

Please leave some feedback by rating this page (1=worst 10=best)! If you have a question, please use the Contact Form to ask me directly via email as I will be able to reply to you if you use that.

Rating 8.25/10 with 12 individual rating(s) given. Minimum rating 3, maximum rating 10

Comments:

6Be warned that the cost of serialisation/unserialisation is very high. I've benchmarked it before when investigating a different system that I wanted to develop.
9Hey pretty cool, I have expanded on this to cope with lots of global variables (stops you having lots of individual files) by adding each one to an array indexed by the name of the variable, and then serializing that array. PHP will allow you to store anything that can be serialized in an array.
9Hey pretty cool, I have expanded on this to cope with lots of global variables (stops you having lots of individual files) by adding each one to an array indexed by the name of the variable, and then serializing that array. PHP will allow you to store anything that can be serialized in an array.
10perfect
10excellent tip, cheers.
10Perfect solution. Worked at my site in one shot. Thanks