Home arrow PHP arrow Page 2 - Caching Result Sets in PHP: Cost-efficient PHP acceleration

The first caching approximation: serializing and unserializing data - PHP

With many websites using a database backend for storing and delivering content, certain common problems arise. One of these is a situation that causes the server to slow down or even bring the system to a complete halt. What can you do to reduce the load on your server? This tutorial discusses one method, a result set caching system, and demonstrates how to implement it with either a procedural or an object-oriented approach.

  1. Caching Result Sets in PHP: Cost-efficient PHP acceleration
  2. The first caching approximation: serializing and unserializing data
  3. A fully functional result set caching system: modifying the original script
  4. Putting the pieces together: implementing the result set caching script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 23
October 03, 2005

print this article



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
// store serialized results in cache file
$fp=fopen($cacheFile,'w') or die('Error opening cache file');
// 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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: