 | | Offres d'emplois |  | développeur php La société Metal Hurlant recherche un(e) stagiaire possédant une solide maîtrise de PHP/MYSQL pour la réalisation d'un jeu de gestion massivement m... | |
 | | Avez vous lu ? |  | | |
|
 La classe pour gérer le cache | |  |

<?
class cache {
// --- répertoire où sont stockés les fichiers du cache
var $cache_dir = "/var/html/cache/";
// --- durée de validité du cache en secondes
var $cache_delay = 86400;
// --- pour ajouter une racine au début du nom des fichiers
var $debut_nom = "";
// --- déclaration des variables utilisées par la classe
var $cache_name = "";
var $cache_file = "";
var $nocache = 0;
var $cache_lastmod = 0;
var $unixdate = 0;
function cache() {
// --- tableau des arguments
$args = array_merge($_POST, $_GET, $_COOKIE);
$args[] = $_SERVER["PHP_SELF"];
$args[] = $_SERVER["HTTP_HOST"];
// --- composition du nom du fichier cache
$this->cache_name = str_replace(" ","",join("_",$args));
$this->cache_name = md5($this->cache_name);
$this->cache_name = $this->debut_nom . $this->cache_name;
// --- nom du fichier
$this->cache_file = $this->cache_dir . $this->cache_name;
}
function get_cache() {
clearstatcache();
$this->unixdate = date("U");
if(file_exists($this->cache_file)) {
$this->cache_lastmod = filemtime($this->cache_file);
$this->cache_expires = $this->cache_lastmod + $this->cache_delay;
$d = $this->unixdate - $this->cache_lastmod;
if ($this->unixdate < $this->cache_expires) {
$txt_cache = join("",file($this->cache_file));
return($txt_cache);
}
}
return false;
}
function set_cache() {
if($this->nocache) return(false);
$buffer = ob_get_contents();
ob_end_flush();
$buffer .= chr(10) . '<!-- Page issue du cache -->';
// --- ecriture du cache dans le fichier
if($fp = fopen($this->cache_file,"w")){
fputs($fp,$buffer);
fclose($fp);
return(1);
}
return(0);
}
}
?>
|
|
|
 |
|