2011-04-28 07:49:27 +00:00
|
|
|
<?php
|
|
|
|
class Cache
|
|
|
|
{
|
|
|
|
protected $maxTime = 8;
|
|
|
|
protected $extension = '.tpl';
|
|
|
|
protected $filename = null;
|
|
|
|
protected $cache_loaded = false;
|
|
|
|
protected $file_loaded;
|
|
|
|
protected $cache = array();
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Initialise le cache avec un nom
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2011-04-28 07:49:27 +00:00
|
|
|
public function __construct($name = null)
|
|
|
|
{
|
2012-11-05 15:33:01 +00:00
|
|
|
$this->filename = Zend_Registry::get('config')->config->path->cache.'/'.$name.$this->extension;
|
2011-04-28 07:49:27 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Détermine si le fichier de cache existe et qu'il n'est pas périmé
|
|
|
|
*/
|
2011-04-28 07:49:27 +00:00
|
|
|
public function exist()
|
|
|
|
{
|
|
|
|
//Fichier inexistant
|
|
|
|
if ( !file_exists($this->filename) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//Ficher timeover
|
|
|
|
if ( $this->timeover($this->filename) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Encode les données pour les placer dans le cache
|
|
|
|
* @param unknown_type $array
|
|
|
|
*/
|
2011-05-26 15:41:36 +00:00
|
|
|
public function setBlock($array)
|
2011-04-28 07:49:27 +00:00
|
|
|
{
|
|
|
|
//Ajout des blocs sérialisés
|
|
|
|
if(!empty($this->filename)){
|
2011-05-26 15:41:36 +00:00
|
|
|
$data = base64_encode(serialize($array));
|
2011-04-28 07:49:27 +00:00
|
|
|
if( !$this->_writefile($data, $this->filename) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Récupére les données du cache et les décode
|
|
|
|
*/
|
2011-05-26 15:41:36 +00:00
|
|
|
public function getBlock()
|
2011-04-28 07:49:27 +00:00
|
|
|
{
|
|
|
|
$cache = $this->_readfile($this->filename);
|
2011-05-26 15:41:36 +00:00
|
|
|
if(isset($cache)){
|
2011-04-28 07:49:27 +00:00
|
|
|
//Traitement
|
2011-05-26 15:41:36 +00:00
|
|
|
$block = unserialize(base64_decode($cache));
|
2011-04-28 07:49:27 +00:00
|
|
|
return $block;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Détermine suivant la date de fichier si celui-ci est périmé
|
|
|
|
*/
|
2011-04-28 07:49:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Supprime le fichier de cache
|
|
|
|
*/
|
2011-04-28 12:43:59 +00:00
|
|
|
public function deletefile()
|
2011-04-28 07:49:27 +00:00
|
|
|
{
|
2011-06-08 09:54:47 +00:00
|
|
|
if(file_exists($this->filename))
|
|
|
|
return unlink($this->filename);
|
2011-04-28 07:49:27 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Lit une ligne dans le fichier
|
|
|
|
*/
|
2011-04-28 07:49:27 +00:00
|
|
|
protected function _readfile()
|
|
|
|
{
|
|
|
|
if ( ($this->filename != $this->file_loaded) ) {
|
|
|
|
$this->file_loaded = $this->filename;
|
|
|
|
if (is_file($this->filename)) {
|
2011-05-26 15:41:36 +00:00
|
|
|
$cache = file_get_contents($this->filename);
|
2011-06-08 07:49:39 +00:00
|
|
|
$this->cache = $cache;
|
2011-04-28 07:49:27 +00:00
|
|
|
}
|
|
|
|
$this->cache_loaded = true;
|
2011-06-08 07:49:39 +00:00
|
|
|
} else {
|
|
|
|
$cache = $this->cache;
|
2011-04-28 07:49:27 +00:00
|
|
|
}
|
|
|
|
return $cache;
|
|
|
|
}
|
|
|
|
|
2011-06-08 07:49:39 +00:00
|
|
|
/**
|
|
|
|
* Ecrit une ligne dans le fichier
|
|
|
|
* @param string $line
|
|
|
|
*/
|
2011-04-28 07:49:27 +00:00
|
|
|
protected function _writefile($line)
|
|
|
|
{
|
|
|
|
$fp = fopen($this->filename,'a');
|
|
|
|
$result = fwrite($fp,$line."\n");
|
|
|
|
fclose($fp);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|