Home arrow PHP arrow Page 5 - Caching Result Sets in PHP: Object Interaction Within a Caching System

Chaining the links: putting the classes together - PHP

In this article, we work directly with a standalone caching class, showing how it interacts with other objects. We will work with aggregation and composition to achieve our goals, which include implementing a time expiry based caching mechanism.

  1. Caching Result Sets in PHP: Object Interaction Within a Caching System
  2. The first link in the caching process: looking at the “Cache” class
  3. The second link in the caching process: overview of the “MySQL” class
  4. The third link in the caching process: a quick look at the “Result” class
  5. Chaining the links: putting the classes together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
October 17, 2005

print this article



Until now, we’ve covered in detail the different classes needed to make the “Cache” class work. The next thing to do is to put all the pieces together and implement a practical example. First, let’s list the complete source code for each class, and then implement the example. The full code listing is as follows:

// class MySQL
class MySQL{
            var $conId; // connection identifier
            var $host; // MySQL host
            var $user; // MySQL username
            var $password; // MySQL password
            var $database; // MySQL database
            // constructor
            function MySQL($options=array()){
                        // validate incoming parameters
                                   foreach($options as $parameter=>$value){
                                               (!empty($value))?$this->{$parameter}=$value:$this->isError('Invalid parameter '.$parameter);
                                   // connect to MySQL
                        else {
                                   $this->isError('No connection parameters were provided');
            // connect to MYSQL server and select database
            function connectDB(){
                                    $this->isError('Error connecting to the server');
                                    $this->isError('Error selecting database');
            // perform query
            function query($query){
                                   $this->isError('Error performing query '.$query);
                        // return new Result object
                        return new Result($this,$this->result);
            // display errors
            function isError($errorMsg){
                       trigger_error($errorMsg.' '.mysql_error());
// class Result
class Result{
            var $mysql; // instance of MySQL object
            var $result; // result set
            // constructor
            function Result(&$mysql,$result){
            // fetch row
            function fetchRow(){
                        return mysql_fetch_array($this->result,MYSQL_ASSOC);
            // count rows
            function countRows(){
                                   $this->mysql->isError('Error counting rows');
                        return $rows;
            // count affected rows
            function countAffectedRows(){
                                   $this->mysql->isError('Error counting affected rows');
                        return $rows;
            // get ID from last inserted row
            function getInsertID(){
                                   $this->mysql->isError('Error getting ID');
                        return $id;
            // seek row
            function seekRow($row=0){
                                   $this->mysql->isError('Error seeking data');
// class Cache
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'){
>expiry=$expiry:$this->mysql->isError('Expire time must be a
positive integer');
            // 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
query. Must start with SELECT');
                                   // read from MySQL
                        else {
                                   // read from cache file
            // write cache file
            function write(){
opening cache file');
to lock cache file');
writing to cache file');
                        return $content;
            // read cache file
            function read(){
reading from cache file');
                        return $content;
            // determine cache validity based on a time expiry
            function isValid(){
                                   return true;
                        return false;
            // fetch cache row
            function fetchRow(){
                                   return false;
                        return $row;
            // fetch all cache rows
            function fetchAll(){
accessing cache data');
                        return $this->data;
            // count cache rows
            function countRows(){
counting cache rows');
                        return $rows;

Although the code is rather lengthy, keep in mind that each class can be included as a separated file, when they’re required. Having a wealth of classes that do the hard work for us, implementing a result set caching system is as simple as this:

// connect to MySQL
$db=new MySQL(array('host'=>'host','user'=>'user','password'=>'password',
// instantiate a new Cache object, valid for 24 hours (time
expiry triggered caching)
$cache=&new Cache($db,86400);
// perform query and store results in cache file
// if cache is not valid, force a new cache generation
$cache->query('SELECT * FROM users');
// loop over rows and display results
            echo $row['firstname'].'&nbsp;'.$row['firstname'].'<br />';
// count rows
echo 'Total number of rows ' .$cache->countRows();

The above example obtains a result set from MySQL, by getting rows from a sample “users” database table, and stores the data in the cache file for 24 hours. All of the requests that occur during that time will fetch data directly from the cache, avoiding query executions.

When the time expiry is reached, the data is again retrieved from MySQL and stored in the cache file. Since the “Cache” class allows you to perform additional tasks, we’ve counted the total number of rows, as you can see in the example.

Say that we want to fetch all of the rows at once. The process is accomplished like this:

// get all rows
// display data or do something else
foreach($rows as $row){
            echo $row['firstname'].'&nbsp;'.$row
['firstname'].'<br />';


Isn’t that simple? Definitely. The “Cache” class is very flexible, as you’ll probably agree. Due to its extensible nature, it might be improved by adding some other methods, useful for manipulating cached data.

Wrapping up

Finally, we’re finished. In this fourth part of the series, we’ve successfully made all of the required classes work together, implementing a time expiry based caching mechanism, useful for reducing the workload associated with multiple query execution.

Since the object-oriented solution has proven to be efficient in caching result sets, in the next part of the series we’ll port the whole code that we’ve worked with to PHP 5. Certainly, the topic is really exciting, so get ready for the next challenge. See you soon!

>>> 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: