159 lines
3.6 KiB
PHP
159 lines
3.6 KiB
PHP
<?php
|
|
class Cache
|
|
{
|
|
protected $maxTime = 8;
|
|
protected $extension = '.tpl';
|
|
protected $filename = null;
|
|
protected $cache_loaded = false;
|
|
protected $file_loaded;
|
|
protected $cache = array();
|
|
|
|
/**
|
|
* Initialise le cache avec un nom
|
|
* @param string $name
|
|
*/
|
|
public function __construct($name = null)
|
|
{
|
|
$this->filename = Zend_Registry::get('config')->profil->path->shared.'/cache/'.$name.$this->extension;
|
|
}
|
|
|
|
/**
|
|
* Détermine si le fichier de cache existe et qu'il n'est pas périmé
|
|
*/
|
|
public function exist()
|
|
{
|
|
//Fichier inexistant
|
|
if ( !file_exists($this->filename) ) {
|
|
return false;
|
|
}
|
|
//Ficher timeover
|
|
if ( $this->timeover($this->filename) ) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Encode les données pour les placer dans le cache
|
|
* @param unknown_type $array
|
|
*/
|
|
public function setBlock($array)
|
|
{
|
|
//Ajout des blocs sérialisés
|
|
if(!empty($this->filename)){
|
|
$data = base64_encode(serialize($array));
|
|
if( !$this->_writefile($data, $this->filename) ) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Récupére les données du cache et les décode
|
|
*/
|
|
public function getBlock()
|
|
{
|
|
$cache = $this->_readfile($this->filename);
|
|
if(isset($cache)){
|
|
//Traitement
|
|
$block = unserialize(base64_decode($cache));
|
|
return $block;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Détermine suivant la date de fichier si celui-ci est périmé
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Supprime le fichier de cache
|
|
*/
|
|
public function deletefile()
|
|
{
|
|
if(file_exists($this->filename))
|
|
return unlink($this->filename);
|
|
else
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Lit une ligne dans le fichier
|
|
*/
|
|
protected function _readfile()
|
|
{
|
|
if ( ($this->filename != $this->file_loaded) ) {
|
|
$this->file_loaded = $this->filename;
|
|
if (is_file($this->filename)) {
|
|
$cache = file_get_contents($this->filename);
|
|
$this->cache = $cache;
|
|
}
|
|
$this->cache_loaded = true;
|
|
} else {
|
|
$cache = $this->cache;
|
|
}
|
|
return $cache;
|
|
}
|
|
|
|
/**
|
|
* Ecrit une ligne dans le fichier
|
|
* @param string $line
|
|
*/
|
|
protected function _writefile($line)
|
|
{
|
|
$fp = fopen($this->filename,'a');
|
|
$result = fwrite($fp,$line."\n");
|
|
fclose($fp);
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Caching WorldCheck Data using Zend_cache
|
|
*
|
|
* @param array $config (elements: path, name, lifetime)
|
|
* @param object $class (object of calling class)
|
|
* @param string $function (name of member function)
|
|
* @param array $param (arguments of member function)
|
|
* @param string $file_name (cache filename)
|
|
* @return mixed (array object)
|
|
*/
|
|
public function wcCache($config, $class, $function, $param, $file_name)
|
|
{
|
|
//caching
|
|
$cache_dir = $config['path'].$config['name'];
|
|
if (!is_dir($cache_dir)) mkdir($cache_dir, 0777, true);
|
|
|
|
$frontendOptions = array(
|
|
'lifetime' => $config['lifetime'],
|
|
'automatic_serialization' => true
|
|
);
|
|
$backendOptions = array('cache_dir' => $cache_dir);
|
|
$cache = Zend_Cache::factory('Output','File',$frontendOptions,$backendOptions);
|
|
|
|
if(!($cache_data = $cache->load($file_name))) {
|
|
$cache_data = new stdClass();
|
|
$cache_data = call_user_func_array(array($class, $function), array($param));
|
|
$cache->save($cache_data, $file_name);
|
|
}
|
|
//end
|
|
return $cache_data;
|
|
}
|
|
} |