In a previous post I showed you how to cache a script’s output using native PHP functions. The class I used in that example was actually inspired by the Zend cache class. If you don’t have Zend installed in your server read this post , it’s easy!
This first thing you should know is that Zend let’s you cache using different front ends and back ends. The front ends include Output, Function, File and Page. I like Output because it let’s me specify what part of the page I want to cache using curly brackets, so we are going to go with the Output front end option.
The back end options include File, Sqlite, Memcached, Apc, Xcache, Zendplatform, Twolevels and Zendserver. The file back end is the easiest to use, it stores the cache in a file, checks if the file exists and if it does it serves the cache, so we are going to use this back end.
Let’s get going. The first thing you want to do is include the cache class
require_once('Zend/Cache.php');
To make a cache object Zend requireds the “factory” method which takes four parameters in the following order. Front end (string), back end (string), front end options (array) and back end options (array).
$cache = Zend_Cache::factory('Output','File',$frontendOptions,$backendOptions);
The only front end option we will be using is “lifetime”, or the time you want to cache output in seconds. The back end option for this tutorial will be the cache folder path.
So far your PHP file should look like this.
// include the cache class require_once('Zend/Cache.php'); // front end options, cache for 1 minute $frontendOptions = array( 'lifetime' => 60 ); // backend options $backendOptions = array( 'cache_dir' => 'cache' // Directory where to put the cache files ); // make cache object $cache = Zend_Cache::factory( 'Output', 'File', $frontendOptions, $backendOptions );
Every cache has to have a unique id. This id has to start with a letter and it can’t include any symbols, but it can have numbers. Am just going to use the string “user1” for my id. I guess this could be a profile page for a user with ID one in a social network.
$cacheID='user1';
You can rename the $cacheID variable to anything you want.
The cache object we made has only two methods that you have to worry about, start() and end() and the syntax to use them is pretty simple.
if (!($cache->start($cacheID))) { // everything from here to the end() method will be cached $cache->end(); }
The start method checks if the cache exists, if it does it includes the cache right there in the “if” statement line. If a cache file with the $cacheID does not exist the cache engine begins to cache everything inside the “if” up until the $cache->end() function.
So the full caching mechanism look like this.
<?php // include the class require_once('Zend/Cache.php'); // front end options $frontendOptions = array( 'lifetime' => 60 ); // backend options $backendOptions = array( 'cache_dir' => 'cache' // Directory where to put the cache files ); // make object $cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions); // make an id $cacheID='user1'; // everything before this is not cached if (!($cache->start($cacheID))) { // begin cache // end cache $cache->end(); } // everything after this is not cached ?>
Important: Everything before or after the if statement is not cached, use this to your advantage 😉
Also make sure that the cache folder exists or the script won’t run.