PHP
  Home arrow PHP arrow Page 5 - Caching Result Sets in PHP: Implementi...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This last installment of the series shows how each class fits in the overall caching...
       · Hi,thank you for this nice series. It was very useful and interesting.I've found...
       · Hello,Thank you for the comments on my PHP series. That's very much...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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