Home arrow PHP arrow Page 2 - Building a Template Parser Class with PHP, Part II

One step forward: defining caching methods - PHP

In part one of this two part article series, you learned how to build a simple template parser class in PHP. In this second article, you will learn how to add caching capabilities to the class.

TABLE OF CONTENTS:
  1. Building a Template Parser Class with PHP, Part II
  2. One step forward: defining caching methods
  3. Putting the pieces together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 28
March 29, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

 

The first question that came to my mind when I decided to implement a caching mechanism was: is it really worth having those capabilities? The answer was a resounding yes! Letís think about it. If weíre working with websites that donít change their content very often, say on a daily basis, itís highly desirable to have a caching system that simply reads the contents to be displayed from a flat file, and delivers them directly to the browser, without the need to process a template file each time the requested page has to be rendered. That sounds like common sense..

 

With this conclusion firmly in mind, I quickly started implementing those desirable caching capabilities inside the class. To keep things ordered, I defined two class methods for reading and retrieving data from a specific cache file, specifying a time-based cache validity policy. The first caching method, "readCache()" simply reads data from a cache file, and is defined in the following way:

 

function readCache($cacheFile,$expireTime){

    if(file_exists($cacheFile)&&filemtime($cacheFile)>
(time()-$expireTime)){

          return file_get_contents($cacheFile);

    }

    return false;

}

 

As you can appreciate, the above method takes two parameters: the cache file and time expiry, respectively, allowing us to specify a value expressed in seconds for setting the proper cache validness.

 

The method checks simultaneously if there is a cache file in the system and that the time expiry has not been reached. Accordingly, if the cache file exists and is valid, the contents from the file are returned. Otherwise, the method returns false. Certainly, youíll agree with me that this is not rocket science.

 

Now, letís take a look at the next method, "writeCache()", which, not surprisingly writes contents to a specified cache file:

 

function writeCache($cacheFile,$content){

    $fp=fopen($cacheFile,'w');

    fwrite($fp,$content);

    fclose($fp);

}

 

On the opposite side, this method accepts a cache file name, and the contents to be written on it as parameters. It naturally opens the file, writes the contents (overriding any previous values) and closes the file. The method is straightforward and easy to understand.

 

At this point, weíre armed with two methods to read and write cache files in an efficient manner. Itís time place them into the class puzzle.

 



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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