Location: PHPKode > scripts > Cached XSL > cached-xsl/class.xslt.inc
<?
//////////////////////////////////////////////////////////
//  CachedXSL class v 1.0.1 build 040710
//
//  XSL Transformation wrapper with cacheing
//  Copiright (C) 2004, Gregory A. Rozanoff
//////////////////////////////////////////////////////////
require_once 'Cache.php';				// PEAR::Cache

class cXSL {

 var
 	$cache_type	= 'file',
	$cache_dir	= "/localhost/tmp/cache/",	// Cache path for PEAR::Cache
	$expireTime	= 86400;			// Cache expiretion time

 function cXSL ($xml, $xsl) {				// Constructor
	$cache = new Cache($this->cache_type, array('cache_dir' => $this->cache_dir));
	$cache_key = $cache->generateID($xml.$xsl);	// Create cache ID

	$this->start = $this->getmicrotime();		// Start performance measuring
	if (! $this->_OUT = $cache->get($cache_key)) {
		$args = array("/_xml" => $this->summon($xml), "/_xsl" => $this->summon($xsl));
		$processor = xslt_create();		// Start XSLT processor
		$this->_OUT = @xslt_process($processor, 'arg:/_xml', 'arg:/_xsl', NULL, $args) or die ('XSL trasformation error #'.xslt_errno($processor).": ".xslt_error($processor));
		xslt_free($processor);			// Stop XSLT processor
		$cache->save($cache_key, $this->_OUT, time() + $this->expireTime);
	} else $this->cached = TRUE;
	$this->stop = $this->getmicrotime();		// Stop terformance measuring
 }

 function done() {					// Destructor
 	$uptime = round(($this->stop - $this->start) * 1000000) / 1000;
	$cached = $this->cached ? "From cache" : "Not cached";

 	echo $this->_OUT."\n\n<!--\n\tExecution Time: {$uptime} ms\n\t{$cached}\n-->";
	unset($this);
	flush();
 }

 function getmicrotime() {
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec); 
 }

 function summon($path) {
	$fp = @fopen($path, 'r') or die ("File ".$path." not exists");
	$content = @fread($fp, @filesize($path));
	fclose($fp);
	return $content;
 }
 
}

?>
Return current item: Cached XSL