Location: PHPKode > scripts > Cached TemplatEze > cached-templateze/dcc.php
<?
////////////////////////////////////////////////////////////////
// Dynamic Content Cache
// (c) Gregory A. Rozanoff, 2004
////////////////////////////////////////////////////////////////
class dcc extends tpl {

 var
	$cache_ext = '.cache';		// Default extension for cache files

 function dcc($tpl = 'default', $tpl_set = 'default') {
	$this->tm = $this->_getmicrotime();
	$this->tpl = $tpl;
	$this->TemplateDir = PATH."/templates/";
	$this->CacheDir = PATH."/cache/";
	$this->_VER_ = $tpl_set;
	
	$context = $tpl."_".$tpl_set;
	$this->cache_key = $this->CacheDir.md5($context).$this->cache_ext;
	
	ob_start(GZIP ? 'ob_gzhandler' : NULL);
	ob_implicit_flush(FALSE);
 }
 
 function done() {
 	$stop = $this->_getmicrotime();
	$uptime = round(($stop - $this->tm) * 1000000) / 1000;

	if (isset($this->TMPL)) print $this->TMPL;
	print "\n\n<!-- Page generated over $uptime mS -->";
	ob_end_flush();
	unset($this);
 }

 function cache_start($eTime = 24) {		// Expire Time in Hours Default - 1 day
	$this->expireTime = $eTime * 3600;
	if ($this->_cached()) {
		readfile($this->cache_key);
		return FALSE;
	}
	$this->TMPL = $this->summon($this->tpl);
	return TRUE;
 }

 function cache_stop($umask = '0111') {		// Store output buffer into cache
	umask($umask);
	$temp_name = tempnam($this->CacheDir, "dcc-");
	$fp = @fopen($temp_name, "wb");
	fputs($fp, $this->TMPL);		// Store content in the temporarily file
	fclose($fp);
	if (file_exists($this->cache_key)) {	// Following manipulation seems strange :)
		rename($this->cache_key, $this->cache_key."~");
		rename($temp_name, $this->cache_key);
		unlink($this->cache_key."~");
	} else  rename($temp_name, $this->cache_key);
 }
 
 function _cached() {				// Check expiration of cache file
	if(CACHE && file_exists($this->cache_key))
		return time() < filemtime($this->cache_key) + $this->expireTime ? TRUE : FALSE;
	return FALSE;
 }

}
?>
Return current item: Cached TemplatEze