This class can be used to cache content in files.
It checks whether a given cache file exists. If it exists and is not expired, the class returns
the cache file contents. If it has expired, the class removes the cache file.
If the cache file does not exist, the class can create a new cache file with from fresh content.
The path of the directory where the cache files are stored is configurable.
/*------------------------------------------------------------------------------------------ CONTENT CACHER CLASS -------------------- Dev By Leon 2007.10.12. Budapest copyright 2007 LeonCreatives - gabor szilagyi Licenced under the GNU Lesser General Public License (Version 3) http://www.gnu.org/licenses/lgpl.html Usage: ------ $cache = new contentCache($_SERVER); $cache->makeExpiration(); if ($cache->status==true) { echo $cache->content; } else { //Create any content to cache! $html="<h1>foo</h1>"; $cache->makeCache($html); echo $html; } ------------------------------------------------------------------------------------------*/ //Class definition class contentCache { //Url witch contains information about requested page public $url = ""; //Method type of request public $method=""; //Path to cache files public $path="cache/"; //Cache contents public $content=""; //Result status of cached contents public $status=false; //Data array to store the $_SERVER assoc. private $DATA = null; //Prepared filename of the cache file private $fname=""; //Last modification timestamp of the cache file private $last_mod_time=0; //Timestamp of now private $now = 0; //Used for to calculate the interval of cache time //www.cpworld2000.com private $exp_min = 0; //Expiration time limit private $exp_max = 3600; //Constructor and main method public function contentCache($data) { if (isset($data)) { $this->DATA=$data; $this->prepareData(); $result = $this->checkContent(); } else { throw new Exception("One of the requested parameters is empty..."); } } private function prepareData() { if ($this->DATA['REQUEST_URI']) { $this->url=$this->DATA['REQUEST_URI']; //OSPHP.COm.CN $this->url=str_replace("/","_",$this->url); $this->url=str_replace("?","_",$this->url); $this->url=str_replace("=","_",$this->url); $this->url=str_replace("&","_",$this->url); if ($this->url!="") { $this->fname=$this->url.".cache"; } } } private function checkContent() { if ($this->fname && is_file($this->path.$this->fname)) { $this->content = file_get_contents($this->path.$this->fname); //PHP开源代码 $this->status=true; } else { $this->content=""; $this->status=false; } } public function makeCache($content="") { if ($content) { file_put_contents($this->path.$this->fname,$content); } else { throw new Exception("No content to cache..."); } return true; } public function makeExpiration() { $this->last_mod_time = filemtime($this->path.$this->fname); //PHP开源代码 $this->now = time(); $this->exp_min=$this->now-$this->last_mod_time; if ($this->exp_min>$this->exp_max) { unlink($this->path.$this->fname); } return true; } //End of the cache class }

No Responses to “Content cache class”