c_cache.php 3.47 KB
<?php

require_once LIBDIR . 'mutualExclusion.php';

class c_cache
{
	private $lock;
	private $cacheFile;
	private $scriptChanged;

	// generiere den Cache Filename aus dem
	// 1. script Namen
	// 2. einem Hash ueber alle verwendeten:
	//  - request parameter
	//  - Werte in GLOBALS
	//  - Sessionvariablen
	// ausserdem wird das cache Verzeichnis auch gleich angelegt wenn es noch
	// nicht existiert.
	function __construct ($params=NULL, $globals=NULL, $session=NULL)
	{
		$cacheDir     = CACHEDIR . substr ($_SERVER['PHP_SELF'], 1);
		$cacheDirArr  = explode ('/', $cacheDir);
		$cacheParms   = array ();
		$cacheGlobals = array ();
		$cacheSession = array ();
		$cachaAllParm = array ();
		$parmStr      = '';

		// cache Verzeichnis anlegen.
		$_dir = '';
		foreach ($cacheDirArr as $dir)
			if (! is_dir ($_dir .= '/' . $dir) && $_dir !== '')
				mkdir ($_dir, 0755);

		unset ($_dir, $dir, $cacheDirArr);

		// alle verwendeten req parameter holen
		if ($params !== NULL)
			foreach ($params as $parm)
				if (isset ($_REQUEST[$parm]))
					$cacheParms[$parm] = $_REQUEST[$parm];
				else
					$cacheParms[$parm] = '';
		ksort ($cacheParms);

		// alle verwendeten globals holen
		if ($globals !== NULL)
			foreach ($globals as $glob)
				if (isset ($GLOBALS[$glob]))
					$cacheGlobals[$glob] = $GLOBALS[$glob];
				else
					$cacheGlobals[$glob] = '';
		ksort ($cacheGlobals);

		// alle verwendeten session variablen holen
		if ($session !== NULL)
			foreach ($session as $sess)
				if (isset ($SESSION[$sess]))
					$cacheSession[$sess] = $SESSION[$sess];
				else
					$cacheSession[$sess] = '';
		ksort ($cacheSession);

		// und nu aus all denen einen md5 hash generieren.
		$cacheAllParm = array_merge ($cacheParms, $cacheGlobals, $cacheSession);

		unset ($cacheParms, $cacheGlobals, $cacheSession);

		foreach ($cacheAllParm as $name => $value)
			$parmStr .= '_' . $name . '=' . $value;

		$parmStr = md5 ($parmStr);

		$this->lock = NULL;
		$this->cacheFile = $cacheDir . '/cache_' . $parmStr . '.html';

		if (file_exists ($this->cacheFile))
		{
			if (filemtime ($this->cacheFile) < filemtime ($scriptFile))
				$this->scriptChanged = TRUE;
			else
			{
				$incFiles = array_unique (array_merge (
					get_included_files (), get_required_files ()));

				foreach ($incFiles as $incFile)
				{
					if (filemtime ($this->cacheFile) < filemtime ($incFile))
					{
						$this->scriptChanged = TRUE;
						break;
					}
				}

				$this->scriptChanged = FALSE;
			}
		}
	}



	function check ($interval=0)
	{
		$scriptFile = HOME . substr ($_SERVER['PHP_SELF'], strlen (URLPATH));

		ob_start ();

		if ($interval <= 0)
		{
			$this->lock = NULL;
			return FALSE;
		}

		print "<pre>";
		var_dump ($this->scriptChanged);
		print "</pre>";

		clearstatcache ();
		if (! $this->scriptChanged &&
				file_exists ($this->cacheFile) &&
				time () - filemtime ($this->cacheFile) < $interval)
		{
			readfile ($this->cacheFile);
			return TRUE;
		}
		else
		{
			$this->lock = acquireLock ($this->cacheFile, '1');

			// inszwischen koennte der cache von einem anderen aufruf
			// aktualisiert worden sein, also nochmal checken.
			clearstatcache ();
			if (file_exists ($this->cacheFile) &&
				time () - filemtime ($this->cacheFile) < $interval)
			{
				releaseLock ($this->lock);
				readfile ($this->cacheFile);
				return TRUE;
			}

			return FALSE;
		}
	}

	function update ()
	{
		$page = ob_get_flush ();

		if ($this->lock !== NULL)
		{
			file_put_contents ($this->cacheFile, $page);
			releaseLock ($this->lock);
		}
	}
};

?>