Suttree.com: Casual Games, Social Software

Read more at the weblog »

PHP DB Cache Overview

This is a class to cache the results of database queries in a simple XML format, which uses a mixture of the query, and the tables involved, to create filenames and directories in which the cached results are stored.

Cache has two main funcitons, search and create.

Set the cache directory using in the search function below.
DOM XML functions are used, and PHP needs to be compiled accordingly (http://www.php.net/manual/en/ref.domxml.php).
The directory name used is md5(query_string) and the filename is md5(tables).

Usage
You do not need to cache all the queries you make, so only use caching when you really need to. For a query that you want to cache, first use search to check whether it already exists. If it does, then use that data. Otherwise, execute and do whatever you want to with your query, then call create so that a cached copy of your query is available, e.g.;

include ("cache_xml.php");
$cache = new Cache;

$query = "SELECT id, name FROM Users";
$tables = "Users";

// See if a cached copy exists, if not, process the query and create the cache after
if($nodes = $cache->search( $query, $tables ) )    {

	// A cached copy exists, so process it...
	print_r($nodes);

} else    {

	// Retrieve the results and process them...
	$result = mysql_query( $query, $link_id );

	// etc

	// Then create a cached copy of the query
	$cache->create( $query, $tables, $result );

} //endif
Installation
Just decompress the file you downloaded to somewhere in your webroot, and the include it from whichever pages you plan to use it in, using include("filename.php").

However, DOM XML functions are used, and PHP needs to be compiled accordingly. You may want to alter the directory where the cached files are stored, adn you can do this by editing the script directly. The variable $this->cache_dir currently points to C:\temp\, and I would recommend changing this before attempting to do anything else.

Download
PHP database caching can be downloaded from here;
Download PHP database caching

Version 2
Version 2 isn't planned, but flames, feedback and questions can be directed to me, here.

Suttree Beatniks with better clothing, brought to you by Duncan Gough. About Archives Casual Game Dev Code Disclaimer EcoLocal Music Knows PMOG Tumblee
Links for Saturday, October 11th, 2008