PHP cache class

< ?php
/**
*
* @var $cache_file_name string file name
* @var $age int age in seconds
*/
class cache {

	public $cache_file_name;
	public $age;

	/**
	* cache::__construct()
	* @access public
	*/
	public function __construct(){
		$this->cache_start();
		register_shutdown_function(array($this, "cache_end"), "inside");
	}

	/**
	* cache::__descruct()
	* @access public
	* @return
	*/
	public function __descruct(){
		$this->cache_end();
	}

	/**
	* cache::cache_start()
	* @access public
	* @return
	*/
	public function cache_start(){

		global $cache_file_name, $age;
		$cache_file_name = 	$_SERVER["DOCUMENT_ROOT"].$_SERVER['REQUEST_URI'] . '_cache';

		if (empty($age)){
			$age = 600;
		}

		if(file_exists($cache_file_name)){
			if (filemtime($cache_file_name) + $age > time()) {
				readfile($cache_file_name);
				unset($cache_file_name);
				exit;
			}
		}
		ob_start();
	}

	/**
	* cache::cache_end()
	* @access public
	* @return
	*/
	public function cache_end()
	{
		global $cache_file_name;
		if (empty($cache_file_name)){
			return;
		}
		$str = ob_get_clean();
		echo $str;
		fwrite(fopen($cache_file_name . '_tmp', "w"), $str);
		rename($cache_file_name . '_tmp',$cache_file_name);
	}

}

?>

Archive for October, 2009

Archives by Month: