checkType('turck'); } default: { $this->type='file'; break; } } } else { $this->type='file'; } $this->setActor(); } # End function pvl_Cache() function cacheExists($key) { if(!empty($key)) { return $this->actor->cacheExists($key); } else { $this->error->processMessage('The "$key" arg is missing in pvl_FileCache::cacheExists.'); return false; } } # Check and set the type of cacheing we are going to use /* private */ function checkType($type) { $temp='file'; switch($type) { case 'turck': { if(function_exists('mmcache_put') && function_exists('mmcache_get')) { $temp=$type; break; } } } $this->type='turck'; } # End function checkType() # Instantiate the class representing our chosen caching method function setActor() { if($this->type=='turck') { $this->actor=&new pvl_TurckCache; } else { $this->actor=&new pvl_FileCache; } $this->actor->error=&$this->error; } # End function setActor # Cache some information function cachePut($key, $val, $ttl='') { if( (!empty($key)) && (!empty($val)) ) { $this->actor->cachePut($key, $val); } else { $this->error->processMessage('Either the $key or $val arg is missing in the pvl_Cache::cachePut Method.'); return false; } } # End function cachePut() # Get information from a cache function cacheGet($key) { if( (!empty($key)) ) { return $this->actor->cacheGet($key); } else { $this->error->processMessage('The $key arg is missing in the pvl_Cache::cacheGet Method.'); return false; } } # End function cacheGet() function cacheRemove($key) { if(!empty($key)) { return $this->actor->cacheRemove($key); } else { $this->error->processMessage('The $key arg is missing in the pvl_Cache::cacheRemove Method.'); return false; } } } class pvl_FileCache { /* private */ var $error=null; var $dir=null; function pvl_FileCache($dir='') { # Let's first check out OS if(stristr($_SERVER['SERVER_SOFTWARE'], 'WIN32')); { $os='windows'; } if(empty($dir)) { $os=='windows' ? $this->dir='c:\tmp\\': $this->dir='/tmp'; } else { if(is_dir($dir)) { $this->dir=$dir; } else { $os=='windows' ? $this->dir='c:\tmp\\': $this->dir='/tmp'; return false; } } return true; } function cacheExists($key) { # Setup our filename first $f_name=(sha1($key)).'.pvlCache'; if(file_exists($this->dir.$f_name)) { return true; } else { return false; } } function cachePut($key, $val) { # Setup our filename first $f_name=(sha1($key)).'.pvlCache'; # Open the file and write to it if successful $fi=fopen($this->dir.$f_name, 'w'); if($fi) { fwrite($fi, serialize($val)); fclose($fi); } else { return false; } } function cacheGet($key) { $c_data=null; # Setup the filename and test for it's existence $f_name=(sha1($key)).'.pvlCache'; if(file_exists($this->dir.$f_name)) { $c_data=unserialize(file_get_contents($this->dir.$f_name)); return $c_data; } else { $msg='The cache file \''.$this->dir.$f_name.'\' does not exist!'; $msg.="\nThis error occurred in pvl_FileCache::cacheGet()\n"; $this->error->processMessage($msg); return false; } } function cacheRemove($key) { # Setup the filename and test for it's existence $f_name=(sha1($key)).'.pvlCache'; if(file_exists($this->dir.$f_name)) { if(unlink($this->dir.$f_name)) { return true; } else { return false; } } # If the file doesn't exist, there is nothing to worry about anyways. } } class pvl_TurckCache { /* private */ var $error=null; var $dir=null; function pvl_TurckCache() {} } ?>