From 4fbc3fa105f9d6eb85dd798a1f1d42615ef594df Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Thu, 28 Apr 2011 07:49:27 +0000 Subject: [PATCH] Ajout class de gestion du cache --- library/Scores/Cache.php | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 library/Scores/Cache.php diff --git a/library/Scores/Cache.php b/library/Scores/Cache.php new file mode 100644 index 000000000..7e3925917 --- /dev/null +++ b/library/Scores/Cache.php @@ -0,0 +1,101 @@ +filename = APPLICATION_PATH.'/../cache/pages/'.$name.$this->extension; + } + + public function exist() + { + //Fichier inexistant + if ( !file_exists($this->filename) ) { + return false; + } + //Ficher timeover + if ( $this->timeover($this->filename) ) { + return false; + } + return true; + } + + public function setBlock($block, $array) + { + //Ajout des blocs sérialisés + if(!empty($this->filename)){ + $data = $block.' = '.serialize($array); + if( !$this->_writefile($data, $this->filename) ) { + return true; + } + } + return false; + } + + public function getBlock($block) + { + $cache = $this->_readfile($this->filename); + if(isset($cache[$block])){ + //Traitement + $block = unserialize($cache[$block]); + return $block; + } else { + return false; + } + } + + protected function timeover() + { + $dateFile = filemtime($this->filename); + $now = mktime(date('G'), date('i'), date('s'), date("m") , date("d"), date("Y")); + $maxTime = mktime( + date('G',$dateFile)+$this->maxTime, + date('i',$dateFile), + date('s',$dateFile), + date("m",$dateFile), + date("d",$dateFile), + date("Y",$dateFile)); + if( $now>$maxTime ) { + return true; + } + return false; + } + + protected function deletefile() + { + if(file_exists($this->file)) + return unlink($this->file); + else + return false; + } + + protected function _readfile() + { + if ( ($this->filename != $this->file_loaded) ) { + $this->file_loaded = $this->filename; + if (is_file($this->filename)) { + $lines = file($this->filename); + foreach ($lines as $line_num => $line) { + list($key, $value) = explode(' = ', $line, 2); + $cache[$key] = $value; + } + } + $this->cache_loaded = true; + } + return $cache; + } + + protected function _writefile($line) + { + $fp = fopen($this->filename,'a'); + $result = fwrite($fp,$line."\n"); + fclose($fp); + return $result; + } +} \ No newline at end of file