Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Caching Result Sets in PHP: Implementing the Caching System in PHP 5
  2. Caching result sets in an PHP 5 scenario: a detailed look at the “Cache” class
  3. Traversing an array structure: developing an array Iterator
  4. Completing the caching application: assembling the relevant classes
  5. A practical approximation: putting the caching application to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
November 07, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: