PHP Partial Cache

basit bir partial cache sınıfı. php5 gerekiyor evet.

Kullanımı aşağıdaki gibi. elbette class ilk olarak include edilmeli evet. ikinci parametre true olarak verilirse o dosya için bir cache yoksa eğer ilk olarak cache oluşturulcak ve tekrar çağrıldığında cache’deki veri dönecek.

 
$output = new outputcache();
 
$output->load("header.php", true);
$output->load("main.php", true);
$output->load("footer.php", true);

bu da sınıf. cache_dir ile tanımlanan klasöre yazma izni verilmeli:

< ?php
 
/*
 * Class: Simple Output Cache v0.2
 * @author: Doruk Özalp
 * 
 * last update:
 * 22 / 01 / 2010 22:20 PM
 * 
 * changelog:
 * v0.2:
 * added file modification time support
 * 
 * v0.1:
 * initial release
 * 
 * 
 */
 
 
class outputcache
{
	//PRIVATE VARIABLES
	private $output = array();
	private $cache_file;
	private $base_dir;
	private $this_dir;
	private $lifetime;
	private $include_file;
        private $cache_dir = "assets/output_cache/";
 
 
	//CONSTRUCTOR
	function __construct()
	{
		$this->setBaseDir($_SERVER["REQUEST_URI"]);
		$this->setThisDir($this->cache_dir . md5($this->getBaseDir()));
		$this->setLifeTime(3600); // 1-hour
	}
 
	//DECONSTRUCTOR
	function __destruct()
	{
 
	}
 
	//PUBLIC FUNCTIONS
 
	public function load($file, $cache = FALSE)
	{
		$this->setIncludeFile($file);
 
		if($cache === FALSE)
		{
			include_once($this->getIncludeFile());
		}
		elseif($cache === TRUE)
		{
			$this->setCacheFile($this->getThisDir() . "/" . md5($this->getBaseDir() . $this->getIncludeFile()));
 
			if($this->checkCacheFile())
			{
				clearstatcache();
 
				if(time() - filemtime($this->getCacheFile()) > $this->getLifeTime())
				{
					$this->makeCacheFile();
				}
				else
				{
					$this->loadCacheFile();	
				}
			}
			else
			{
				$this->makeCacheFile();	
			}
		}
	}
 
	//PRIVATE FUNCTIONS
 
	private function makeCacheFile()
	{
		$this->start();
 
		include_once($this->getIncludeFile());
 
		$this->setOutput($this->getCacheFile(), $this->getContent());
 
		$this->end();
 
		$this->writecachefile();	
	}
 
	private function checkCacheFile()
	{
		$retVal = FALSE;
 
		if(file_exists($this->getCacheFile()))
		{
			$retVal = TRUE;
		}
 
		return $retVal;
	}
 
	private function loadCacheFile()
	{
		echo file_get_contents($this->getCacheFile());
	}
 
	private function writecachefile()
	{
		if(!is_dir($this->getThisDir()))
		{
			mkdir($this->getThisDir(), 0777);
		}
 
		$handle = fopen($this->getCacheFile(), 'w+'); 
		fwrite($handle, $this->getCacheContent($this->getCacheFile()));
		fclose($handle);
	}
 
	private function getCacheContent($file_md5)
	{
		return $this->getOutput($file_md5);
	}
 
	private function start()
	{
		return ob_start();
	}
 
	private function end()
	{
		return ob_end_flush();
	}
 
	private function getContent()
	{
		return ob_get_contents();
	}
 
	//SETTER AND GETTERS
 
	private function setBaseDir($input)
	{
		$this->base_dir = $input;
	}
 
	private function getBaseDir()
	{
		return $this->base_dir;
	}
 
	private function setThisDir($input)
	{
		$this->this_dir = $input;
	}
 
	private function getThisDir()
	{
		return $this->this_dir;
	}
 
	private function setLifeTime($input)
	{
		$this->lifetime = $input;
	}
 
	private function getLifeTime()
	{
		return $this->lifetime;
	}
 
	private function setCacheFile($input)
	{
		$this->cache_file = $input;
	}
 
	private function getCacheFile()
	{
		return $this->cache_file;
	}
 
	private function setOutput($index, $input)
	{
		$this->output[$index] = $input; 
	}
 
	private function getOutput($index)
	{
		return $this->output[$index];
	}
 
	private function setIncludeFile($input)
	{
		$this->include_file = $input;
	}
 
	private function getIncludeFile()
	{
		return $this->include_file;
	}
}
?>