<?php
/* PHP IFB_CACHE CLASS 1.0.1
* Copyright © 2011 Infobull.net, All Rights Reserved
* License: http://www.gnu.org/licenses/gpl.html
* Require php 5.2++
* $Id: ifbCache.php rev 3 $ Ricko $ 2011/01/06
* @charset = 'UTF-8'
*/
class ifbCache
{
const varPFIX = 'c';
public $now;
protected $cacheDir;
protected $cache = array('file' => array(), 'data' => array(), 'save' => array());
protected $cacheExt = array(0 => '.dat', 1 => '.php', 2 => '.gz', 3 => '.inc');
public function __construct($cacheDir = 'cache')
{
$this->cacheDir = $cacheDir.'/';
$this->now = time();
}
/********** PARTIE 1 : Chargement et lecture des fichiers **********/
// public loadFiles( mixed $files, [int $dataType = NULL] )
public function loadFiles($files, $dataType = NULL)
{
if(!is_array($files))
return $this->loadData($files, $dataType, true);
foreach($files as $fileName)
{
$this->loadData($fileName, $dataType);
}
}
// public getVar( string $fileName , string $var )
public function getVar($fileName, $var)
{
return isset($this->cache['data'][$fileName][$var]) ? $this->cache['data'][$fileName][$var] : NULL;
}
// public getData( string $fileName )
public function getData($fileName)
{
return isset($this->cache['data'][$fileName]) ? $this->cache['data'][$fileName] : NULL;
}
// public getAll()
public function getAll()
{
return $this->cache['data'];
}
/********** PARTIE 2 : Mise à jour et sauvegarde des fichiers **********/
public function setVar($fileName, $key, $var, $dataType = 0)
{
if(!isset($this->cache['data'][$fileName]))
$this->loadData($fileName, $dataType, false);
if(!isset($this->cache['file'][$fileName]))
$this->cache['file'][$fileName] = 0;
if(!in_array($fileName, $this->cache['save']))
$this->cache['save'][] = $fileName;
$this->cache['data'][$fileName][$key] = $var;
}
// Func assignVars( $fileName[string], $var[array], isCode[bolean] ) : Insertion du tableau contenant les données à mettre en cache.
// Si nom de fichier inexistant, un nouveau nommé $fileName sera créé lors du save(). Dans le cas contraire, les données sont écrasées
public function setData($fileName, $var, $dataType = 0)
{
if(!isset($this->cache['data'][$fileName]))
$this->checkFileExt($fileName, $dataType);
if(!in_array($fileName, $this->cache['save']))
$this->cache['save'][] = $fileName;
$this->cache['data'][$fileName] = $var;
}
public function saveFiles($result = false)
{
foreach($this->cache['save'] as $fileName)
{
switch($this->cache['file'][$fileName]) {
case 0:
$this->cache['data'][$fileName] = serialize($this->cache['data'][$fileName]);
break;
case 2:
$this->cache['data'][$fileName] = gzcompress($this->cache['data'][$fileName]);
break;
default:
NULL;
};
$saved[] = $this->saveData($fileName);
}
$this->cleanVars(false);
if($result == true)
return 'cacheSaved: '.implode(' ', $saved);
}
/********** PARTIE 3 : Fonctions internes **********/
// protected loadContent( string $fileName , [int $dataType = NULL], [boolean $rData = false] )
protected function loadData($fileName, $dataType = NULL, $rData = false)
{
if(isset($dataType)):
if(!$this->checkFileExt($fileName, $dataType)):
return;
endif;
else:
if(!$this->checkFile($fileName))
return;
endif;
switch($this->cache['file'][$fileName]) {
case 0:
$this->cache['data'][$fileName] = unserialize(file_get_contents($this->cacheDir.$fileName.$this->cacheExt[0]));
break;
case 1:
require $this->cacheDir.$fileName.$this->cacheExt[1];
$tmpName = $this->stringToVar($fileName);
$this->cache['data'][$fileName] = $$tmpName;
break;
case 2:
$this->cache['data'][$fileName] = gzuncompress(file_get_contents($this->cacheDir.$fileName.$this->cacheExt[2]));
break;
case 3:
$this->cache['data'][$fileName] = file_get_contents($this->cacheDir.$fileName.$this->cacheExt[3]);
break;
default:
NULL; // $this->cache['data'][$fileName] = $this->otherDataType();
};
if($rData == true)
return $this->cache['data'][$fileName];
}
protected function saveData($fileName)
{
$fh = @fopen($this->cacheDir.$fileName.$this->cacheExt[$this->cache['file'][$fileName]], 'wb');
if ($fh):
if(is_array($this->cache['data'][$fileName]))
fputs($fh, '<?php '."\n".'$'.$this->stringToVar($fileName).' = '.var_export($this->cache['data'][$fileName], true).'; '."\n".'?>');
else
fputs($fh, $this->cache['data'][$fileName]);
return $fileName;
endif;
fclose($fh);
}
// protected stringToVar ( string $var )
protected function stringToVar($var)
{
return self::varPFIX.preg_replace("/[^a-zA-Z0-9_]/","_", $var);
}
// protected checkFile( string $fileName )
protected function checkFile($fileName)
{
foreach($this->cacheExt as $key => $ext)
{
if(file_exists($this->cacheDir.$fileName.$ext)):
$this->cache['file'][$fileName] = $key;
return true;
endif;
}
return false;
}
// protected checkFileExt( mixed $fileName, [int $dataType = NULL] )
protected function checkFileExt($fileName, $dataType = NULL)
{
if(!isset($dataType) or !array_key_exists($dataType, $this->cacheExt))
return false;
if(!is_array($fileName))
$this->cache['file'][$fileName] = $dataType;
else
foreach($fileName as $tmpName)
{
$this->cache['file'][$tmpName] = $dataType;
}
return true;
}
// public checkFile ( string $fileName, int $dataType, [int $expire = 86400] )
public function checkFileTime($fileName, $dataType, $expire = 86400) // default 86400=24h
{
if(!file_exists($this->cacheDir.$fileName.$this->cacheExt[$dataType]))
return 0;
$file = filemtime($this->cacheDir.$fileName);
$expire = $this->now - intval($expire);
return ($file > $expire ? ($file-$expire) : 1);
}
// nettoyage
public function cleanVars($all = true)
{
if ($all == true)
$this->cache = array('file' => array(), 'data' => array(), 'save' => array());
else
$this->cache['save'] = array();
}
public function __sleep()
{
$this->cleanVars(false);
return array('cache');
}
public function __wakeup()
{
return;
}
} // class end
// preg_match('/a:[0-9]+:\{[aisb]+:(.*?)\}/', $file)
?>