In previous posts full page, block level caching with pear cache lite have been discussed. Full page level caching is fine under a lot of conditions. However, sometimes it is desirable to cache the contents of an array to a file. PEAR Cache Lite makes this very easy.
The array used in the following example is for illustration only and very basic. On a real site the array may be the result of several database queries, or even a recursive function to display the full category structure on a website E.g. something a lot more database intensive and costly than a simple array!
The setup using PEAR Cache Lite is exactly the same, the only difference is within the constructor. There is an option called ‘automaticSerialization’ (set to false by default) that enables the caching of none string data E.g. arrays. It is possible to achieve what following by using PHP’s serialize and unserialze functions, but it’s preferable and neater to use what functionality the author of the PEAR Cache Lite class has provided. The code is very simple.
Firstly, include the PEAR Cache Lite class and pass the associative array of options (including the automaticSerialization key) to the class constructor (line highlighted):
include 'Cache/Lite/Output.php'; $options = array( 'cacheDir' => 'pear_cache/', 'lifeTime' => 3600, 'automaticSerialization' => true ); $cache = new Cache_Lite_Output($options);
Secondly, for simplicity, include a function to generate the array to be cached. As previously mentioned, this array would come from some sort of database intensive operation under normal circumstances:
function letters() { return range('A', 'Z'); }
Thirdly, check for a cache hit or miss. If there is a cache miss, generate the array and save it to a cache file (line 5). Otherwise, there is a cache hit and the cached data can be used directly (line 2):
if( $data_array = $cache->get('array_of_letters') ) { $data =$data_array; } else { $data = letters(); $cache->save($data); }
Lastly, use the array within your application normal (the following example simply outputs each letter with a semi colon after each item):
$count = count($data); for ( $i=0; $i echo $data[$i]; echo ($i != ($count-1)) ? ' : ' : null; }
Looking at the actual cache file (open it using your PHP editor or Wordpad as it has no file extension), you can see the cached array has indeed been serialized (the letters at the beginning of the file are a timestamp and not part of your cached array):
715704231a:26{i:0;s:1:"A";i:1;s:1:"B";i:2;s:1:"C";i:3;s:1:"D"; ... [removed for usability] ... s:1:"Z";}
That’s it! For reference, the full code can viewed below:
include 'Cache/Lite/Output.php'; $options = array( 'cacheDir' => 'pear_cache/', 'lifeTime' => 3600, 'pearErrorMode' => CACHE_LITE_ERROR_DIE, 'automaticSerialization' => true ); $cache = new Cache_Lite_Output($options); function letters() { return range('A', 'Z'); } if( $data_array = $cache->get('array_of_letters') ) { $data =$data_array; } else { $data = letters(); $cache->save($data); } $count = count($data); for ( $i=0; $i echo $data[$i]; echo ($i != ($count-1)) ? ' : ' : null; }
One thought on “PHP Array Caching with PEAR Cache Lite”