Last week I was given the task my a client to speed up their website – they had paid for literally a single hours work, so not much time at all. The site in question was a company website that had various dyamic boxes of content – namely an area for latest events, latest news and latest clients – aswell as a custom content management solution running for the main page content. Additionally, the database is hosted by (for me), argueably the worse and slowest webhost around – streamline.net – so database lookups are slower than usual to begin with.
Each time a page was served, many unecessary requests were being made to the MySQL database. The site gets quite a lot of passing traffic from search engines, with all the content be created byn the site owner. As a result, databse lookups for the same content each time as not needed and some simple caching was to be used – nameley, PEAR Cache_Lite.
PEAR Cache_Lite is a small yet powerful PHP caching system that will cache (or save on disk) our dynamic PHP pages. Subsequent requests for complex and database intensive pages are not required due to our cache. This will speed up the site no end.
Firstly, you’ll need to download the latest Cache_Lite package and include the file Lite.php on your page. Also, create a folder called ‘cache’ within your site structure. The general idea of Cache_Lite is very simple, giving each page a unique ID – there is no set rule for this and the creation of this ID is left to the developer.
The actual code to cache a complex page is very simple:
// Include the Cache_Lite Class require_once 'classes/Lite.php'; // Create a unique ID for the page, in this case, the full URL. You // should stick to a single convention here! $id = md5('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); // Set options and create a new instance of the class - self explantory! $options = array( 'cacheDir' => '\var\sites\s\site.com\cache\', 'lifeTime' => 3600, 'automaticSerialization' = > true ); $cache = new Cache_Lite($options); if ($data = $cache->get($id)) { // If we have a cached version of the page, do not generate a new one } else { // Use PHP output buffering to save the contents of the webpage to a variable, //for use in the cache file ob_start(); echo '<!-- Served From Cache: '. date('l jS \of F Y h:i:s A').' -->'; /* At this point you would create your page as normal - all the HTML/PHP processing code would be here! */ // Save the dynamic page to a variable using the PHP function ob_get_contents $data = ob_get_contents(); // Save the page to a cache file, using the ID created earlier $cache->save($data, $id); ob_get_clean(); } // Echo out the contents of the page echo $data;
Now, load up the page in your browser and everything should look normal. Refresh the page and view the source code, you should have a HTML comment saying:
<!-- Served From Cache: Sunday 7th of November 2010 03:52:42 PM -->
Additionally, in the cacge folder you will have a file with a long name, containing your cached content. This is extremely easy to apply to any page!
One point to note in relation to this example – as all the content is generated by the site administrator you would currently run into problems! The administrator (my client in this case) may update their news for example and the latest news would not be refreshed due to the cache. My aim here is to avoid any uneceassy questions down the road from the client. At this point I added a small piece of code to their CMS that simply clears the cache file when an area of content is added. The code is as follows:
// Code omitted that edits a news item ..... // Generate a link (E.g. the full page URL) to this news item $ID = News::generateLink($this->id, $this->headline); $options = array( 'cacheDir' => '\var\sites\s\site.com\cache\', 'lifeTime' => 30 ); $cache = new Cache_Lite($options); // Delete the cache file with the specified ID $cache->remove($ID); // Rest of code to edit news item
One thought on “PHP Caching with PEAR Cache Lite”