PHP
  Home arrow PHP arrow Page 3 - Caching Result Sets in PHP: The Barebones of a Caching Class
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: The Barebones of a Caching Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2005-10-10


    Table of Contents:
  • Caching Result Sets in PHP: The Barebones of a Caching Class
  • Chaining things along: a quick look at the procedural caching solution
  • The object-oriented solution: developing a result set caching class
  • Caching with class: a deeper look at the “Cache” class
  • More class methods in detail: ending up the round

  • 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: The Barebones of a Caching Class - The object-oriented solution: developing a result set caching class
    ( Page 3 of 5 )

    Prior to developing the caching class, we need to specify some important prerequisites. First, the class will implement a time expiry-based caching trigger. However, this is not a big limitation, since a different caching trigger might be developed without making significant changes to the source code. This will be left as a possible additional feature.

    Second, the class will aggregate a MySQL abstraction object, for having database connectivity within its scope, so if you’re not familiar with the concept of aggregation, feel free to visit Object Interaction in PHP Introduction to
    Aggregation part 1
    , where I’ve written an article series that discusses the topic in detail.

    Finally, since the source code is highly portable, in a future article the whole set of classes will be updated to PHP 5, aimed specifically at those developers working with the latest version of PHP.

    All right, having stated these few disclaimers, it’s time to look at the result caching class, which, not surprisingly, I’ve called “Cache.” Its definition is as follows:

    class Cache{
                var $mysql;  // instance of MySQL object
                var $result; // instance of Result object
                var $expiry; // cache expire time in seconds
                var $cacheFile; // cache file
                var $data; // result set array
                // constructor
                function Cache(&$mysql,$expiry=86400,$cacheFile='default_cache.txt'){
                            $this->mysql=&$mysql;
                            (is_int($expiry)&&$expiry>0)?$this->expiry=$expiry:$this->mysql-
    >isError('Expire time must be a positive integer');
                            $this->cacheFile=$cacheFile;
                            $this->data=array();
                }
                // if cache is valid, perform query and return a result set. Otherwise, get
    results from cache file
                function query($query){
                            // check if query starts with SELECT
                            if(!preg_match("/^SELECT/",$query)){
                                       $this->mysql->isError('Invalid query. Must start with
    SELECT');
                            }
                            if(!$this->isValid()){
                                       // read data from MySQL
                                       $this->result=$this->mysql->query($query);
                                       // write data to cache file
                                       $this->data=$this->write();
                            }
                            else {
                                       // read data from cache file
                                       $this->data=$this->read();
                            }
                }
                // write cache file
                function write(){
                            if(!$fp=fopen($this->cacheFile,'w')){
                                       $this->mysql->isError('Error opening cache file');
                            }
                            if(!flock($fp,LOCK_EX)){
                                       $this->mysql->isError('Unable to lock cache file');
                            }
                            while($row=$this->result->fetchRow()){
                                       $content[]=$row;
                            }
                            if(!fwrite($fp,serialize($content))){
                                       $this->mysql->isError('Error writing to cache file');
                            }
                            flock($fp,LOCK_UN);
                            fclose($fp);
                            unset($fp,$row);
                            return $content;
                }
                // read cache file
                function read(){
                            if(!$content=unserialize(file_get_contents($this->cacheFile))){
                                       $this->mysql->isError('Error reading from cache file');
                            }
                            return $content;
                }
                // determine cache validity based on a time expiry trigger
                function isValid(){
                            if(file_exists($this->cacheFile)&&filemtime($this->cacheFile)>(time
    ()-$this->expiry)){
                                       return true;
                            }
                            return false;
                }
                // fetch cache row
                function fetchRow(){
                            if(!$row=current($this->data)){
                                       return false;
                            }
                            next($this->data);
                            return $row;
                }
                // fetch all cache rows
                function fetchAll(){
                            if(count($this->data)<1){
                                       $this->mysql->isError('Error accessing cache data');
                            }
                            return $this->data;
                }
                // count cache rows
                function countRows(){
                            if(!$rows=count($this->data)){
                                       $this->mysql->isError('Error counting cache rows');
                            }
                            return $rows;
                }
    }

    At first glance, and looking at the data member declaration, we can see that the “Cache” class aggregates a MySQL object and a “Result” object, where the first one is directly passed to the constructor. So, if you might want to see the complete code for each class involved, don’t feel concerned about this. We’ll see in turn, the proper definition for all of the additional classes that we’re working with.

    For the moment, let’s focus our attention on the code for the “Cache” class, which will be explained in detail over the next few lines.



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