Location: PHPKode > projects > Utopia CMS > utopia/core/lib/cache.php
<?php
/**
 * Librairie Cache
 *
 * @package Utopia
 * @subpackage LibCache
*/

defined('INC') or exit;

/**
 * Librairie Cache
 *
 * Cette librairie permet la mise en cache de contenus dynamiques simples, et supporte la gestion de variables dynamiques dans la cache.
 * @package Utopia
 * @subpackage LibCache
*/
class LibCache {
	public $cachePath;
	public $delimit = array('{', '}');
	public $fileExt = '.cache';

	private $_kernel;
	private $page;
	private $delay; // in minutes
	private $buffer = '';
	private $vars = array();

	function __construct($_kernel, $page, $delay = 60) {
		$this -> _kernel = $_kernel;
		$this -> page = $page;
		$this -> delay = $delay;
		$this -> cachePath = $this -> _kernel -> getParam('Cache.Path', 'LibCache');
	}

	public function start() {
		ob_start();
	}

	public function stop() {
		$this -> buffer.= ob_get_clean();
	}

	public function write($buffer = '') {
		$this -> buffer.= $buffer;

		if ($this -> delay > 0)
			return file_put_contents($this -> cachePath.$this -> page.$this -> fileExt, $this -> buffer);
		else
			return TRUE;
	}

	public function isValid() {
		if ($this -> delay > 0) {
			$fileName = $this -> cachePath.$this -> page.$this -> fileExt;

			if (file_exists($fileName) && filemtime($fileName) + $this -> delay * 60 > time())
				return TRUE;
		}

		return FALSE;
	}

	public function register($name, $value) {
		$this -> vars[$name] = $value;
	}

	public function read() {
		if (empty($this -> buffer) && $this -> delay > 0)
			$buffer = file_get_contents($this -> cachePath.$this -> page.$this -> fileExt);
		else
			$buffer = $this -> buffer;

		// Replace dynamic variables.
		$s = preg_quote($this -> delimit[0], '/');
		$e = preg_quote($this -> delimit[1], '/');

		$buffer = preg_replace_callback('/'.$s.'([^'.$e.']+)'.$e.'/', array($this, 'replaceVar'), $buffer);

		return $buffer;
	}

	private function replaceVar($sub) {
		if (isset($this -> vars[$sub[1]]))
			return $this -> vars[$sub[1]];
		else
			return $this -> delimit[0].$sub[1].$this -> delimit[1];
	}
}
?>
Return current item: Utopia CMS