HomePHP Page 5 - Caching Result Sets in PHP: Implementing the Caching System in PHP 5
A practical approximation: putting the caching application to work - PHP
Welcome to the final part of the series “Caching Result Sets in PHP.” Hopefully, the last chapter of this tutorial will help you to put together all of the classes developed in the previous part, and demonstrate how the complete caching system can be implemented in a PHP 5 controlled environment.
As you might guess, since each class is hiding all of the internal processing behind its interface, coding an example to demonstrate the caching system’s functionality is a breeze. The lines below show a simple way of caching result sets:
// include the class files (notice that class files can be included using __autoload for multiple file inclusion) require_once 'mysqlclass.php'; require_once ‘resultclass.php'; require_once 'arrayprocessorclass.php'; require_once 'cacheclass.php';
try { // connect to MySQL $db=new MySQL(array('host'=>'host','user'=>'user','password'=>'password', 'database'=>'databasename')); // instantiate a new Cache object and set cache validity for 24 hours $cache=new Cache($db,86400); // get result set $cache->query('SELECT * FROM users'); // fetch one row at time while($row=$cache->fetchRow()){ echo $row['firstname'].'<br />'; } // display total number of rows echo 'Total number of rows '.$cache->countRows(); // fetch all rows $rows=$cache->fetchAll(); // display data foreach($rows as $data){ echo $data['firstname'].$data ['lastname'].'<br />'; } // fetch ten rows $rows=$cache->fetchRange(0,10); // display data foreach($rows as $data){ echo $data['firstname'].$data ['lastname'].'<br />'; } } // catch thrown exceptions catch(Exception $e){ echo $e->getMessage(); exit(); }
As you can see, the example speaks for itself. To demonstrate the functionality of the caching system, I’ve explicitly used the available methods for fetching result sets. Doing so, the usage of them is clearly demonstrated, in this case by displaying the data from a “users” database table. However, it’s feasible to use one method for data visualization and another for different processing (i.e. sending by email cached data).
Another possibility would be to use the “fetchRange()” method for displaying paginated data. As usual, feel free to add your own application for each explained method. Once the required functionality is available, possibilities are really numerous.
Conclusion
Finally, the journey has ended. Through this series, we’ve explored together the many options that PHP offers for caching result sets. Starting out from the creation of a simple procedural script, to the development of a more advanced object-oriented solution, hopefully the whole experience has been rich both in theory and practice.
Result set caching is a powerful technique that can be implemented to accelerate many websites that deliver quite static content, and PHP exposes a set of nice features to take advantage of caching solutions. Reduced server overload and limited query execution translate directly into faster and more efficient web applications. From a developer’s point of view, there is much to gain in terms of better performance and cost-efficient website acceleration.