Location: PHPKode > scripts > Website Cache > website-cache/websiteCache.php
<?
	class websiteCache{
		var $domain;
		var $pathToCache = '/full/path/to/cache/'; //with trailing slash
		var $historyFile = 'history.txt';
		var $expiry = 5; //time in minutes for a cached file to expire
		
		function cacheInit($domain){
			if ($domain[strlen($domain) - 1] != "/"){
				$domain .= "/";
			}
		
			$this->domain = $domain;
		}
		
		function newCache($file){
			/* Get the history */
			$handle = fopen($this->pathToCache . $this->historyFile, 'r');		
				
			$history = '';			
			while (!feof($handle)) {
				$history .= fread($handle, 8192);
			}			
			
			fclose($handle);	
			
			$pos = strpos($history, $file);
			
			$historyArray = array();
			$historyArray = explode('|', $history);		
			
			if ($pos === false){			
				// hasn't been cached before, make a cache

				$handle = fopen($this->domain . $file, 'r');			
				$cache = '';			
				while (!feof($handle)) {
					$cache .= fread($handle, 8192);
				}			
				fclose($handle);
				
				if ($handle = fopen($this->pathToCache . $file, 'w+')){
					if (fwrite($handle, $cache)){
						fclose($handle);	
										
						$historyArray[sizeof($historyArray)] = $file .','.time();
						//update history
						if ($handle = fopen($this->pathToCache . $this->historyFile, 'w+')){
							$historyText = '';
							
							foreach($historyArray as $v){
								$historyText .= $v.'|';
							}
							
							$historyText = substr($historyText, 0, strlen($historyText) - 1);
						
							if (fwrite($handle, $historyText)){
								return true;
							}else{
								echo 'History could not be saved.';
								return false;
							}
						}else{
							echo 'History file missing!';
							return false;
						}						
					}else{
						fclose($handle);
						echo 'Data could not be written to cache!';
						return false;
					}
				}else{
					fclose($handle);
					echo 'File could not be created.';
					return false;
				}
			}else{
				// it has been cached before. check if its up to date				
				
				foreach ($historyArray as $i => $v){
					$pos = strpos($v, $file);
					
					if ($pos === false){}
					else{

						$data = explode(',', $v);
						
						$timeSinceCache = time() - $data[1];
						
						if ($timeSinceCache > ($this->expiry * 60)){
							//need a recache
							
							$handle = fopen($this->domain . $file, 'r');			
							$cache = '';			
							while (!feof($handle)) {
								$cache .= fread($handle, 8192);
							}			
							fclose($handle);
							
							if ($handle = fopen($this->pathToCache . $file, 'w+')){
								if (fwrite($handle, $cache)){
									$historyArray[$i] = $file .',' . time();
									
									//update history
									if ($handle = fopen($this->pathToCache . $this->historyFile, 'w+')){
										$historyText = '';
										
										foreach($historyArray as $v){
											$historyText .= $v.'|';
										}
									
										$historyText = substr($historyText, 0, strlen($historyText) - 1);
									
										if (fwrite($handle, $historyText)){		
											return true;
										}else{
											echo 'History could not be saved.';
											return false;
										}
									}else{
										echo 'History file missing!';
										return false;
									}	
								}else{
									echo 'Data could not be written to cache!';
									return false;
								}
							}else{
								echo 'File could not be created.';
								return false;
							}
						}else{
							return true;
						}												
					}
				}
			}
		}
		
		function printCache($file){
			if (file_exists($this->pathToCache . $file)){						
				include($this->pathToCache . $file);
			}else{
				echo 'Cache not found.';
			}
		}
	}
?>
Return current item: Website Cache