PHP
  Home arrow PHP arrow Page 2 - Building a Template Parser Class with PHP, Part II
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building a Template Parser Class with PHP, Part II
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 27
    2005-03-29


    Table of Contents:
  • Building a Template Parser Class with PHP, Part II
  • One step forward: defining caching methods
  • Putting the pieces together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building a Template Parser Class with PHP, Part II - One step forward: defining caching methods
    ( Page 2 of 3 )

     

    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek