Ajout class de gestion du cache

This commit is contained in:
Michael RICOIS 2011-04-28 07:49:27 +00:00
parent afd7614f22
commit 4fbc3fa105

101
library/Scores/Cache.php Normal file
View File

@ -0,0 +1,101 @@
<?php
class Cache
{
protected $maxTime = 8;
protected $extension = '.tpl';
protected $filename = null;
protected $cache_loaded = false;
protected $file_loaded;
protected $cache = array();
public function __construct($name = null)
{
$this->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;
}
}