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

The first link in the caching process: looking at the “Cache” class - 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



In order to get a better understanding of the interaction process between classes, let’s remind ourselves of how the “Cache” class looks, since it’s the first relevant structure within the caching system. To refresh our memories, here is the complete definition for this class:

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');
                        else {
            // 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;

So far, so good. Since the class has been explained in detail over the third part of the series, I won’t stop long on it. Let’s take a brief look at some of its most important features, before jumping directly into the source code for the other classes.

As we’ve seen before, the “Cache” class allows you to store an entire result set in a cache file for a given time period. Once the expiry has been reached, a new query is run for getting “fresh” data, and then the cache generation is carried out again. As long as the cached data is valid, it will be read from the file, being available for some kind of further processing.

Asides from the regular methods to handle programmatically the cached data, the class exposes some additional methods for fetching either single rows or multiple rows, as well as the ability to count rows.

Indeed, these features don’t present any difficulties. However, there are some interesting things within the class definition. Note that database connectivity tasks, query execution, and error handling are delegated to different classes. This implicitly means that there must be other objects that are passed to the “Cache” class for carrying out these specific operations.

Essentially, we can see that all of the database work is done by a MySQL object, which is passed to the class by its constructor. Therefore, it’s time to take a look at the MySQL abstraction class, responsible for connecting to MySQL, selecting databases and running queries.

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