Login   Register  
PHP Classes
elePHPant
Icontem

File: class.CachedFastTemplate.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jesus M. Castagnetto  >  CachedFastTemplate  >  class.CachedFastTemplate.php  >  Download  
File: class.CachedFastTemplate.php
Role: ???
Content type: text/plain
Description: Class allowing caching of Fastemplate based pages
Class: CachedFastTemplate
Simple class extending CDI's Fastemplate Class
Author: By
Last change:
Date: 14 years ago
Size: 5,008 bytes
 

Contents

Class file image Download
<?php

/*
 * Class CachedFastTemplate
 * by Jesus M. Castagnetto (jesusmc@scripps.edu)
 * (c) 2000. Version 1.1
 *
 * Description:
 * This class extends CDI's (cdi@thewebmasters.net) FastTemplate class,
 * implementing methods to cache the output to a file. This would be
 * useful for cases in which a template is used for both, the static
 * pages in a web site, and pages created from PHP scripts. In this
 * way we will avoid processing pages that do not change too often.
 *
 * Changes:
 * 2000/06/06 - Merged in some modifications submitted by Aaron Bush
 *              (AB) <abush@microcenter.com>. In fact he sent
 *              these some time ago, and I finally unearth them from
 *              my filesystem. Mainly changed to recognize if the
 *              the file calling this class or the templates have 
 *              been modified.
 * 2000/06/07 - Added some logic, so the same template can be used for
 *              storing a cached template for seconds/minutes/hours/days
 *              added 2 new methods: set_time_unit and _is_valid_time_unit,
 *              and renamed internal methods (the ones with names prefixed
 *              by "_"), from _mkdatestamp to _mktimestamp, and from
 *              _diff_days to _diff_time, renamed/modified also some internal
 *              variables in methods.
 * 
 */


class CachedFastTemplate extends FastTemplate {

    var $CACHEDIR = "./cache";      // directory to save cached files
    var $CACHELENGTH = 30;          // length of caching, 30 units.
	var $TIMEUNIT = "day";			// time unit
	var $TIMEUNITSARR = array ("sec"=>1, "min"=>60, "hour"=>3600, "day"=>86400);

    function CachedFastTemplate($path_to_tpls="", $cdir="", $clen="") {
        $this->FastTemplate($path_to_tpls);
        if (!empty($cdir)) {
            $this->set_cache_dir($cdir);
        }
        if (!empty($clen)) {
            $this->set_cache_length($clen);
        }
    }

    function set_cache_dir($dir) {
        if (substr($dir,(strlen($dir) - 1), 1) == "/") {
            $dir = substr($dir,0,-1);
        }
        $this->CACHEDIR = $dir;
    }

    function set_cache_length($length) {
        $this->CACHELENGTH = $length;
    }

    function write_to_cache($filename="", $content="") {
        global $PHP_SELF;
        if (empty($filename)) {
            $filename = str_replace("/", "_", $PHP_SELF);
        }
        if (empty($content)) {
            $content = $this->fetch();
        }
        // write the contents
        $fp = fopen($this->CACHEDIR."/".$filename.".cache", "w");
        fwrite($fp,$content);
        fclose($fp);
        // write the cache control file
        $timestamp = $this->_mktimestamp();
        $fp = fopen($this->CACHEDIR."/".$filename.".cntrl", "w");
        fwrite($fp, $timestamp.":".$this->CACHELENGTH);
        fclose($fp);
    }

    function read_from_cache($filename="") {
        global $PHP_SELF;
        if (empty($filename)) {
            $filename = str_replace("/", "_", $PHP_SELF);
        }
        if ($this->is_cached($filename)) {
            readfile($this->CACHEDIR."/".$filename.".cache");
            return true;
        } else {
            return false;
        }
    }

    function is_cached($filename) {
        return (is_file($this->CACHEDIR."/".$filename.".cache") &&
                is_file($this->CACHEDIR."/".$filename.".cntrl"));
    }

    function valid_cache_file($filename="") {
        global $PHP_SELF, $PATH_TRANSLATED;		// for new logic (AB)
        if (empty($filename)) {
            $filename = str_replace("/", "_", $PHP_SELF);
        }
        if ($this->is_cached($filename)) {
            $info = file($this->CACHEDIR."/".$filename.".cntrl");
            $val = explode(":", $info[0]);
			// the following block from (AB)
			// check fileHandles time vs. control time
 			if (filemtime($PATH_TRANSLATED) >= $val[0])
 				return false;
 			if (count($this->FILELIST) > 0 ) {
 				while (list($tag, $fn) = each($this->FILELIST)) {
 					if ( is_file($this->ROOT.$fn) &&
 						(filemtime($this->ROOT.$fn) >= $val[0]) )
 							return false;
 				}
 			}
 			// end check fileHandles
 
            $today = $this->_mktimestamp();
            return ( $this->_diff_time($today, $val[0]) <= $val[1] );
        } else {
            return false;
        }
    }

	function set_time_unit($str) {
		if ($this->_is_valid_time_unit($str)) {
			$this->TIMEUNIT = $str;
		} else {
			$this->TIMEUNIT = "day";
		}
	}

	function _is_valid_time_unit($str) {
		while (list($key, $val) = each($this->TIMEUNITSARR)) {
			if ($key == $str)
				return true;
		}
		return false;
	}	

    function _mktimestamp() {
		return time();
    }

    function _diff_time($end, $start) {
		$factor = $this->TIMEUNITSARR[$this->TIMEUNIT];
        return intval(($end - $start)/$factor);
    }

} // end of class definition
?>