. */ class pCache { protected $HashKey = ""; protected $CacheFolder = "Cache/"; /** * Create the pCache object */ public function __construct($CacheFolder = "Cache") { $this->CacheFolder = $CacheFolder.'/'; } /** * Clear the cache folder */ public function ClearCache() { if($handle = opendir($this->CacheFolder)) { while(false !== ($file = readdir($handle))) { if($file != "." && $file != "..") unlink($this->CacheFolder.$file); } closedir($handle); } } /** * Check if we have an offline version of this chart */ public function IsInCache($ID, $Data, $Hash = '') { if($Hash === '') $Hash = $this->GetHash($ID, $Data); return file_exists($this->CacheFolder.$Hash); } /** * Make a copy of drawn chart in the cache folder */ public function WriteToCache($ID, $Data, pChart $Picture) { $Hash = $this->GetHash($ID, $Data); $FileName = $this->CacheFolder.$Hash; imagepng($Picture->getPicture(), $FileName); } /** * Remove any cached copy of this chart */ public function DeleteFromCache($ID, $Data) { $Hash = $this->GetHash($ID, $Data); $FileName = $this->CacheFolder.$Hash; if(file_exists($FileName)) unlink($FileName); } /** * Retrieve the cached picture if applicable * * @param string $ID ID/short string of the Picture * @param pData $Data pChart->getData ;) * @param bool $return FALSE prints the image and exits TRUE returns the picture * @return string image/PNG If $return == TRUE, the image is returned */ public function GetFromCache($ID, $Data, $return = FALSE) { $Hash = $this->GetHash($ID, $Data); if(!$this->IsInCache('', '', $Hash)) return false; $FileName = $this->CacheFolder.$Hash; if($return) { return file_get_contents($FileName); } else { header('Content-type: image/png'); @readfile($FileName); exit (); } } /** * Build the graph unique hash key */ protected function GetHash($ID, $Data) { $mKey = "$ID"; foreach($Data as $Values) { $tKey = ""; foreach($Values as $Serie => $Value) $tKey = $tKey.$Serie.$Value; $mKey = $mKey.md5($tKey); } return (md5($mKey)); } }