MySQL
  Home arrow MySQL arrow Page 4 - Writing Your Own Template Caching Class In...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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? 
MYSQL

Writing Your Own Template Caching Class In PHP
By: Havard Lindset
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2002-11-19

    Table of Contents:
  • Writing Your Own Template Caching Class In PHP
  • How Is It Supposed To Work?
  • The Code Explained (contd.)
  • The Code Explained (contd.)
  • Real-World Examples
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Writing Your Own Template Caching Class In PHP - The Code Explained (contd.)


    (Page 4 of 6 )

    if (is_file($filename)) {
    clearstatcache();

    if (filemtime($filename) > (time() - $this->_cacheLifetime)) {
    $isCached = true;
    }
    }


    The first "if" construct simply checks if the file exists. The second if construct checks if the time since the file was last modified is greater than $_cacheLifetime. Let's move on to the _addCache() method. This method simply creates a cache file:

    function _addCache($content, $file, $id = false)
    {
    $id = $id ? md5($id . basename($file)) : md5(basename($file));
    $filename = $this->_cacheDir . '/' . $id . '/' . basename($file);
    $directory = $this->_cacheDir . '/' . $id;

    @mkdir($directory, 0775);

    if ($fp = fopen($filename, 'wb')) {
    fwrite($fp, $content);
    fclose($fp);
    }
    }


    As you can see, this code snippet is pretty similar to the code in the isCached() method.

    @mkdir($directory, 0775);

    The above function call simply creates the directory for the cache item.

    if ($fp = fopen($filename, 'wb')) {
    fwrite($fp, $content);
    fclose($fp);
    }


    This code opens a stream to the cache file (which is created there and then), and writes everything inside $content to the file. There's actually only one method left before the class is finished. The last method we'll add is _getCache(). This method is used for retrieving the content of a cache file:

    function _getCache($file, $id = false)
    {
    $id = $id ? md5($id . basename($file)) : md5(basename($file));
    $filename = $this->_cacheDir . '/' . $id . '/' . basename($file);

    if ($fp = fopen($filename, 'rb')) {
    $content = fread($fp, filesize($filename));
    fclose($fp);
    }

    return isset($content) ? $content : false;
    }


    Here's the only differing part from the last function:

    if ($fp = fopen($filename, 'rb')) {
    $content = fread($fp, filesize($filename));
    fclose($fp);
    }

    return isset($content) ? $content : false;


    This code simply opens a stream to the cache fil, and reads it all into a variable. The variable is then returned. That's it! The class is now finished.

    The Whole Class
    Here's the whole class in one part. Just in case you're in a hurry:

    <?php

    class Template {

    var $_variables = array();
    var $_caching = false;
    var $_cacheDir = '/cache';
    var $_cacheLifetime = 120;

    function addVar($name, $value)
    {
    $variables = &$this->_variables;
    $variables[$name] = $value;
    }

    function display($file, $id = false)
    {
    echo $this->fetch($file, $id);
    }

    function fetch($file, $id = false)
    {
    if ($this->_caching == true && $this->isCached($file, $id)) {
    $output = $this->_getCache($file, $id);
    } else {
    $output = $this->_getOutput($file);

    if ($this->_caching == true) {
    $this->_addCache($output, $file, $id);
    }
    }

    return isset($output) ? $output : false;
    }

    function _getOutput($file)
    {
    extract($this->_variables);
    $file = realpath($file);

    if (file_exists($file)) {
    ob_start();
    include($file);
    $output = ob_get_contents();
    ob_end_clean();
    } else {
    trigger_error("Failed opening the template file '$file'. The file doesn't exist.", E_USER_ERROR);
    }

    return !empty($output) ? $output : false;
    }

    function setCacheDir($dir)
    {
    $dir = realpath($dir);

    if (is_dir($dir) && is_writable($dir)) {
    $this->_cacheDir = $dir;
    } else {
    trigger_error("The cache directory '$dir' either doesn't exist, or it cannot be written to by PHP", E_USER_WARNING);
    $this->_cacheDir = '';
    $this->_caching = false;
    }
    }

    function setCacheLifetime($seconds)
    {
    if (is_numeric($seconds)) {
    $this->_cacheLifetime = $seconds;
    }
    }

    function setCaching($state)
    {
    if (is_bool($state)) {
    $this->_caching = $state;
    }
    }

    function isCached($file, $id = false)
    {
    $id = $id ? md5($id . basename($file)) : md5(basename($file));
    $filename = $this->_cacheDir . '/' . $id . '/' . basename($file);

    if (is_file($filename)) {
    clearstatcache();

    if (filemtime($filename) > (time() - $this->_cacheLifetime)) {
    $isCached = true;
    }
    }

    return isset($isCached) ? true : false;
    }

    function _addCache($content, $file, $id = false)
    {
    $id = $id ? md5($id . basename($file)) : md5(basename($file));
    $filename = $this->_cacheDir . '/' . $id . '/' . basename($file);
    $directory = $this->_cacheDir . '/' . $id;

    @mkdir($directory, 0775);

    if ($fp = fopen($filename, 'wb')) {
    fwrite($fp, $content);
    fclose($fp);
    }
    }

    function _getCache($file, $id = false)
    {
    $id = $id ? md5($id . basename($file)) : md5(basename($file));
    $filename = $this->_cacheDir . '/' . $id . '/' . basename($file);

    if ($fp = fopen($filename, 'rb')) {
    $content = fread($fp, filesize($filename));
    fclose($fp);
    }

    return isset($content) ? $content : false;
    }
    }

    ?>


    I'll now show you a few examples on advanced usage of this class in real-world PHP applications.

    More MySQL Articles
    More By Havard Lindset


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT