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


       · This part of the series goes through the makings of the basic structure for a...
       · Am I the only one who only sees part 1 and 3?
       · Hello,Thank you for commenting on the article. And regarding your question,...
       · The second part was scheduled for today, but an editing mix-up placed the third...
       · The second part of the article was published today. Here is the link...
       · Thank you very much for your easy to understand articles. I am using much code...
       · Another solution for my case would involve creating a db table named...
       · Hello,Thanks for commenting on the article. It's always nice to get your...
       · Hello again,Regarding your caching alternative, try to keep your database access...
       · Thank you for your input. It gives me a better understanding of the usage for this...
       · Hello,Thank you for the compliments on the series I wrote, and I'm glad to hear...
     

       

    PHP ARTICLES

    - 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
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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