Home arrow PHP arrow Page 4 - Caching Result Sets in PHP: A Content-Change Triggered Caching System

Wrapping the code: defining the “readData()” and “readQueryResult()” functions - PHP

Caching within the context of PHP application acceleration can be triggered based on three possible categories: time expiry, content change, and manually. This article covers an application that triggers the caching mechanism based on a content change condition.

  1. Caching Result Sets in PHP: A Content-Change Triggered Caching System
  2. Back to the origins: a quick overview of the time expiry based caching script
  3. Another caching strategy: building a content-change triggered caching system
  4. Wrapping the code: defining the “readData()” and “readQueryResult()” functions
  5. Gluing the pieces together: listing the whole source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 12, 2005

print this article



Probably wrapping the above code into a couple of containing functions won’t have a big impact on its functionality, but it helps to keep the code more compact and readable. Again, this is a matter of personal preference. Whatever the case, we might define a “readData()”, function which contains most of the logic implemented in the caching script, as well as a “readQueryResult()” function, useful for connecting to MySQL, running a query and returning a result set in the form of an array. Doing so, the “readQueryResult()” function would look like this:

function readQueryResult($options=array()){
            // check parameters
                        trigger_error('Invalid number of parameters');
            // create connection variables
            foreach($options as $key=>$value){
            // connect to MySQL
                        trigger_error('Error connecting to the server '.mysql_error());
            // select database
                       trigger_error('Error selecting database '.mysql_error());
            // check if query starts with SELECT
                        trigger_error('Invalid query. Must start with SELECT');
                       trigger_error('Error performing query '.$query.mysql_error());
            return $data;

And the “readData()” function would be defined as follows:

function readData($options=array(),$cacheFile='default_cache.txt'){
            // check to see if cache file is valid (content changed triggered caching)
                                   // read randomly data from MySQL
                                   // read cache data
                                   // compare cached data with fresh data
                                   // if content has changed then force new cache generation
                                   // read data from cache file
                        // read data from MySQL
                        // store data in cache file
            return $data;

The first function simply connects to MySQL, selects the database and runs a SELECT query, returning the result set as an array. The second function implements the core logic of the script, as you can clearly appreciate.

Having defined the wrapping functions, the script can be run as easily as this:

// read data from MySQL or cache file
'databasename','query'=>'SELECT * FROM users');
// display data
foreach($data as $key=>$row){
            echo 'First Name :'.$row['firstname'].' Last Name :'.$row
['lastname'].'<br />';


When the script is first run, the data will be returned as a result set after performing the SQL query, and then stored in the cache file. The subsequent requests to the script will return data either from the database or the cache file, depending on the randomly generated number.

As you can see, the caching trigger might be easily changed to obey different rules. One possible addition would be writing a function that runs the query after a specified number of script executions, or by defining a function that manually deletes the cache file, thereby forcing a new cache generation. Possibilities are certainly numerous.

For those developers that want to play around with the above listed code, just jump straight to the next section, where its complete list is shown.

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