PHP
  Home arrow PHP arrow Page 5 - Caching Result Sets in PHP: Implementing the Caching System in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Caching Result Sets in PHP: Implementing the Caching System in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2005-11-07


    Table of Contents:
  • Caching Result Sets in PHP: Implementing the Caching System in PHP 5
  • Caching result sets in an PHP 5 scenario: a detailed look at the “Cache” class
  • Traversing an array structure: developing an array Iterator
  • Completing the caching application: assembling the relevant classes
  • A practical approximation: putting the caching application to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Caching Result Sets in PHP: Implementing the Caching System in PHP 5 - A practical approximation: putting the caching application to work
    ( Page 5 of 5 )

    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT