Saving Resources with phpCache

Once youíve uploaded the file, youíll need to include it at the start of the PHP scripts on which you want to use it, like this:

include("../phpCache.inc");

Now that youíve installed the script, and have it in place in the scripts you wish to use it with, letís learn how to use it to cache your data.

Setting up the Caching Function

There is one main function you need to be familiar with, and that is cache(). This function takes three parameters: time, object, and key, and itís used like this:

if (!($et=cache(time, object, key))){ 
. 
. 
. 
endcache(); 
}

The first line above uses the $et variable defined by the caching script to check if there is a cache available. If there isnít, the block of code that follows it will be executed. It is in that block of code that you need to include the information you want cached (such as your database queries). Finally the endcache() function, which saves and closes the cache, is executed. Your data wonít be saved without the endcache() function, so be sure to include it.

There is one important detail you need to know before you set this up on your page. If youíll be running any queries outside the cache, in addition to the ones youíll run inside the cache, donít establish your database connection in the cached block of code. Place it above the code block instead -- this way your non-cached queries will still run.

Now we need to fill in the arguments for the cache() function. The first argument, time, is pretty straight forward: itís the duration of the cache in seconds. You can set this argument to any number, from 60 seconds to 6 million seconds. If you set the time argument to 0, the cache will never time out.

The next arguments really work together, as theyíre both used to store and identify the cached data. The object argument usually holds the name or URL of the script. The key argument usually holds an identifier thatís not related to the script name, such as the query string, form variables, or the primary key from your database, as discussed above. To store and retrieve your cached data, phpCache will cross reference the object and the key.

There is also another function for caching, called cache_all(), which can be used like this:

if (!($et=cache_all(time))){ 
. 
. 
. 
endcache(); 
}

The obvious difference with this function is that it only takes one argument, time. The reason it doesnít have an object or key argument is that it uses defaults for those fields. The defaults are generated by two functions youíll find in your phpCache.inc file. One, cache_default_key(), we mentioned above. This function generates a key based on form variables and your query string. The object default is generated by the cache_default_object() function, and simply returns the host and filename of the script.

In many cases, you can use these defaults. There was the exception I talked about above, where you may want to edit or just not use the default key, and in cases where you want to share the same cached data across multiple pages, you wonít want to use the default object. These attributes can be easily replaced with any identifier you choose.