PHP
  Home arrow PHP arrow Page 2 - Caching Result Sets in PHP: Cost-efficient PHP acceleration
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? 
Google.com  
PHP

Caching Result Sets in PHP: Cost-efficient PHP acceleration
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 21
    2005-10-03


    Table of Contents:
  • Caching Result Sets in PHP: Cost-efficient PHP acceleration
  • The first caching approximation: serializing and unserializing data
  • A fully functional result set caching system: modifying the original script
  • Putting the pieces together: implementing the result set caching script

  • 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: Cost-efficient PHP acceleration - The first caching approximation: serializing and unserializing data
    ( Page 2 of 4 )

    Definitely, one of the nicest features of PHP is the ability to serialize and unserialize data. This two-way process is easily accomplished by the function "serialize()" and its counterpart "unserialize()". In short, when a PHP value is serialized, a string representation of its type and structure is obtained. This is useful for storing different data structures as byte-stream strings, and then, by reversing the operation, restoring them to their original types and structure.

    These operations are commonly used when working with objects, when one or more objects are serialized to work with sessions, saving their properties but not the methods. However, using a workaround, it's possible to restore an object from a serialized state and still have its methods available.

    As PHP allows you to quickly serialize several values, we'll use this ability as the primary base for building a result set caching script. The logic of the script is fairly easy to understand by the following sequence: we connect to MySQL and perform a SELECT statement, retrieving a result set, whose values are saved into an array. Then, we simply serialize the array, and finally store it in a cache file. Simple, right?

    According to the above explanation, a basic version of a result set caching script would look like this:

    // connect to MySQL
    $db=mysql_connect('host','user','password') or die('Error
    connecting to the server '.mysql_error());
    // select database
    mysql_select_db('databasename',$db) or die('Error selecting
    database '.mysql_error());
    // perform query
    $result=mysql_query('SELECT * FROM users') or die('Error
    performing query '.mysql_error());
    // store rows in array
    while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
        $results[]=$row;
    }
    // store serialized results in cache file
    $cacheFile='cacheFile.txt';
    $fp=fopen($cacheFile,'w') or die('Error opening cache file');
    fwrite($fp,serialize($results));
    fclose($fp);
    unset($fp,$cacheFile);
    // display rows
    foreach($results as $key=>$row){
        echo $row['firstname'].'&nbsp;'.$row['lastname'].'<br />';
    }

    As you can see, the script is extremely simple in its current incarnation. It connects to MySQL, and executes a SELECT statement against a sample "users" table, obtaining a result set. Next, it saves the data in the "results" array, which is serialized and stored in a sample cache file. Doing so, a byte-stream representation of the array is now saved in the given file.

    Finally, the data is displayed using a regular "foreach" loop, showing the first and last name of some selected users. Indeed the whole script flow is easy to follow.

    Despite the fact that the snippet is very simple, it's quite good at illustrating the core logic behind this result set caching system. However, until now nothing has been gained in terms of improved performance, for the sole reason that the script is not taking advantage of the cached data. Each time the code is executed, the same steps will be carried out, storing the result set in the cache file.

    Surely you'll agree that, with a result set saved to a file, it's fairly easy to introduce some modifications to the above script and make the cache work properly. Therefore, let's change some of the script's code and put the caching system to work.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek