extranet/library/Scores/Cache.php
Damien LASSERRE 15b99fdaf3
2011-06-03 08:53:22 +00:00

124 lines
2.7 KiB
PHP

<?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;
}
/**
* Pour des apelles statics.
*
* @param unknown_type $action
* @param unknown_type $siret
* @param unknown_type $id
*/
public function getCacheOBJ($action, $siret, $id)
{
$filename = APPLICATION_PATH.'/../cache/pages/'.$action.'-'.$siret.'-'.$id.'.tpl';
if (file_exists($filename)) {
$dateFile = filemtime($filename);
$now = mktime(date('G'), date('i'), date('s'), date("m") , date("d"), date("Y"));
$maxTime = mktime(
date('G',$dateFile) + 8,
date('i',$dateFile),
date('s',$dateFile),
date("m",$dateFile),
date("d",$dateFile),
date("Y",$dateFile));
if( $now < $maxTime ) {
return (unserialize(base64_decode(file_get_contents($filename))));}
return false;
}
return (false);
}
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;
}
public function getBlock()
{
$cache = $this->_readfile($this->filename);
if(isset($cache)){
//Traitement
$block = unserialize(base64_decode($cache));
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;
}
public 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)) {
$cache = file_get_contents($this->filename);
}
$this->cache_loaded = true;
}
return $cache;
}
protected function _writefile($line)
{
$fp = fopen($this->filename,'a');
$result = fwrite($fp,$line."\n");
fclose($fp);
return $result;
}
}