Location: PHPKode > scripts > Ole File Cache > fileCache.php
<?php

// fileCache v1.2 by: Ole Kristian Karlsen
//
// June 16th 2010.
//
// Usage:
//
// 1. Try to load cache (Returns false on failure)
// 2. If load returned false, generate cache, save cache.
// 3. If load returned data, parse data, Else parse generated data.
//


class fileCache
{
	static public function load($fileName, $allowedLifeTime, $forcedLoad = false)
	{
		$fileData = false;

		if (self::hasValidAge($fileName, $allowedLifeTime, null, $forcedLoad))
		{
			$fileData = file_get_contents($fileName);
		}

		return $fileData;
	}

	static public function save($fileName, $fileData)
	{
		$bytesWritten = file_put_contents($fileName, $fileData, LOCK_EX);

		if ($bytesWritten !== false) return true;
		else return false;
	}

	static public function hasValidAge($fileName, $allowedLifeTime, $forcedExpiration = false, $forcedValidation = false)
	{
		$status = false;

		if ($forcedExpiration) $status = false;
		elseif (($forcedValidation) && (file_exists($fileName))) $status = true;

		else
		{
			$lifeTime = self::fileAge($fileName);
			if ($lifeTime < $allowedLifeTime) $status = true;
		}

		return $status;
	}

	static public function fileAge($fileName)
	{
		return time() - self::timeModified($fileName);
	}

	static public function timeModified($fileName)
	{
		$uTime = 0;

		clearstatcache();

		if (file_exists($fileName))
		{
			$uTime = filemtime($fileName);
		}

		return $uTime;
	}
}

?>
Return current item: Ole File Cache