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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: