/*
* Class CachedFastTemplate
* by Jesus M. Castagnetto (jesusmc@scripps.edu)
* (c) 2000. Version 1.0
*
* 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.
*/
class CachedFastTemplate extends FastTemplate {
var $CACHEDIR = "./cache"; // directory to save cached files
var $CACHELENGTH = 30; // length of caching, 30 days.
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
$datestamp = $this->_mkdatestamp();
$fp = fopen($this->CACHEDIR."/".$filename.".cntrl", "w");
fwrite($fp, $datestamp.":".$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"));
}
$filename = "yipeee.html";
// if the filename is empty, the class will use PHP_SELF
// Check if we can send the cached file
if ($tpl->valid_cache_file($filename)) {
echo "<B>FROM CACHE</B>\n<BR>";
$tpl->read_from_cache($filename);
$end = $tpl->utime();
$runtime = ($end - $start)*1000;
echo "Completed in $runtime miliseconds<BR>\n";
exit;
// Otherwise process the page
// the templates are distributed w/FastTemplate
$tpl->define(
array(
main => "main.tpl",
table => "table.tpl",
row => "row.tpl"
)
);
$tpl->assign( array( TITLE => "FastTemplate Test") );