Saving Resources with phpCache

Installing phpCache

First, download the script. Unzip it -Ė the only file you need is View the other included files at your leisure: they may give you ideas on different ways to use the script, but for the purposes of this article they arenít needed.

Once youíve extracted the file, consider renaming it. Itís a bad idea to put any script that doesnít end with the proper file extension in any public area on your server. This is because there could be sensitive information in that file, and if a user tried to access it directly, theyíd be able to: if the file doesnít have the correct extension, the code inside it will be displayed in the browser. So itís a good idea to either rename the file using a .php extension, or store it in a directory above your root public directory.

There are a few things youíll need to edit inside the file itself. Open the file in your favorite text editor and look for this line:

define(CACHE_DIR, "/tmp/phpCache/");

Unless you have your own server youíll need to edit that code. You should make a tmp directory above your root html directory, and inside it place a phpCache directory. Then enter the path to your new directory like so:

define(CACHE_DIR, "/home/username/tmp/phpCache/");

If you donít know the path to your directory, ask your server administrator or log into a shell session and use the pwd command.

The other thing you may need to edit is the key function. phpCache uses a key to store the cached data. The key is generated by default using GET and POST variables as well as your query string. If youíre using another method to pass information to your dynamic pages, then youíll need to edit this function in order to take those variables into account.

Two variables you might be using are $PATH_INFO or $REQUEST_URI -- if you are using them, change your cache_default_key() function (found in the file) to this:

function cache_default_key() { 
 return md5("GET=" . serialize($HTTP_GET_VARS) . "QS=" .  

Alternatively you can specify what you want to key with, if you opt to use the cache(), rather than cache_all(), function when doing your caching. You can also choose to key the cache based on something else, such as your database primary key, if you use the cache() function. More about this later.