PHP
  Home arrow PHP arrow Page 2 - 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 - Caching result sets in an PHP 5 scenario: a detailed look at the “Cache” class
    ( Page 2 of 5 )

    To complete the process of developing the caching system, we have to analyze in detail the core “Cache” class and understand properly its logic. Although this class was already reviewed in the previous part of the series, it’s useful to refresh our memory and take a look at its definition. That way, it will be easier to explain how the array processor class fits into the programming schema.

    The list for the “Cache” class is the following:

    class Cache{
                private $mysql;  // instance of MySQL object
                private $result; // instance of Result object
                private $arrproc; // instance of arrayProcessor
    object
                private $expiry; // cache expire time in seconds
                private $cacheFile; // cache file
                private $data; // result data array
                // constructor
                public function __construct
    (&$mysql,$expiry=7200,$cacheFile='default_cache.txt'){
                            $this->mysql=&$mysql;
                            $this->expiry=$expiry;
                            $this->cacheFile=$cacheFile;
                            $this->data=array();
                }
                // if cache is valid, perform query against database. Otherwise, get results from cache file
                public function query($query){
                            if(!$this->isValid()){
                                       $this->result=&$this->mysql-
    >query($query);
                                       // read data from MySQL
                                       $this->data=$this->write();
                            }
                            else {
                                       // read data from cache file
                                       $this->data=$this->read();
                            }
                            // create a new instance of
    arrayProcessor object
                            $this->arrproc=new arrayProcessor($this-
    >data);
                }
                // write cache file
                private function write(){
                            if(!$fp=fopen($this->cacheFile,'w')){
                                       throw new Exception('Error
    opening cache file');
                            }
                            // lock cache file
                            if(!flock($fp,LOCK_EX)){
                                       throw new Exception('Unable to
    lock cache file');
                            }
                            // get result array from query result
                            while($row=$this->result->fetchRow()){
                                       $contents[]=$row;
                            }
                            // write serialized data to cache file
                            if(!fwrite($fp,serialize($contents))){
                                       throw new Exception('Error
    writing to cache file');
                            }
                            // unlock cache file
                            flock($fp,LOCK_UN);
                            fclose($fp);
                            unset($fp,$row);
                            // return query result
                            return $contents;
                }
                // read cache file
                private function read(){
                            if(!$contents=unserialize
    (file_get_contents($this->cacheFile))){
                                                   throw new
    Exception('Error reading cache file');
                            }
                            // return cached data
                            return $contents;
                }
                // determine if cached data is valid or not (time expiry triggered cache)
                private function isValid(){
                            if(file_exists($this->cacheFile)
    &&filemtime($this->cacheFile)>(time()-$this->expiry)){
                                       return true;
                            }
                            return false;
                }
                // fetch row
                public function fetchRow(){
                            if(!$row=$this->arrproc-
    >getCurrentElement()){
                                       return false;
                            }
                            $this->arrproc->getNextElement();
                            return $row;
                }
                // fetch all rows
                public function fetchAll(){
                            $this->arrproc->countElements();
                            return $this->data;
                }
                // count rows
                public function countRows(){
                            return $this->arrproc->countElements();
                }
                // fetch range of rows
                public function fetchRange($offset,$length){
                            return $this->arrproc->getRange
    ($offset,$length);
                }
    }

    As you’ve seen before, the class essentially handles the logic for reading a result set either from MySQL or from the specified cache file, based on a time expiry caching policy. Due to the fact that the class hides the internal processing behind the “query()” method, it’s possible to do a little refactoring and implement a different cache trigger or even a combination of triggers.

    Asides from handling the caching trigger, the class exposes several additional methods for single and multiple row fetching, as well as for counting rows and returning a range of them. It’s easy to see that each of the methods that performs array operations, uses the methods provided by the array processor object.

    This is best understood by taking a look at the following line, within the “query()” method:

    // create a new instance of arrayProcessor object
    $this->arrproc=new arrayProcessor($this->data);

    Whenever this method is invoked, it instantiates an object from the “arrayProcessor” class, which is directly fed with the array obtained from the cache file, in case you have a valid caching period, or from the query resource.

    Notice that the array processor object’s methods are used to fetch or count rows. However, it’s fairly easy to add more methods for more complex result set manipulation. For instance, suppose that we need to return a randomly selected row from a result set. This would be quickly done by using a method of the array processor that picks up a random element. Indeed, expanding the “Cache” class’ functionality would be a pretty straightforward process.

    Since the “Cache” class relies heavily on the array processor object, it’s necessary to look at its structure. Doing so, the concepts explained above will be better grasped.



     
     
    >>> 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 1 hosted by Hostway
    Stay green...Green IT