* @author Aurélien PERONNET * @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org) * @license http://framework.zend.com/license/new-bsd Licence New BSD * @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL * @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL * @version $Id$ * @link /doc/framework/ */ // TODO : voir ce qui est le plus pratique : error_log ou le gestionnaire de bogue du framework class CacheSimple { private $mise_en_cache = null; private $stockage_chemin = null; private $duree_de_vie = null; private $options = array( 'mise_en_cache' => 'true', 'stockage_chemin' => 'Fichier::getDossierTmp()', 'duree_de_vie' => '3600*24' ); public function __construct($options = array()) { extract($options); $this->mise_en_cache = is_bool($mise_en_cache) ? $mise_en_cache : $mise_en_cache == '' ? false : true; if ($this->mise_en_cache) { $this->stockage_chemin = isset($stockage_chemin) ? realpath($stockage_chemin) : Fichier::getDossierTmp(); if (!realpath($stockage_chemin)) { error_log(sprintf("%s: Attention, %s invalide: creation [%s]", __FILE__, $stockage_chemin, @$_SERVER['REQUEST_URI'])); mkdir($stockage_chemin, 0755, TRUE); } if (!realpath($stockage_chemin)) { error_log(sprintf("%s: Attention, realpath(%s) invalide [%s]", __FILE__, $stockage_chemin, @$_SERVER['REQUEST_URI'])); } else if(!is_writable(realpath($stockage_chemin))) { error_log(sprintf("%s: Attention, realpath(%s) non-inscriptible [%s]", __FILE__, realpath($stockage_chemin), @$_SERVER['REQUEST_URI'])); } else { $this->duree_de_vie = isset($duree_de_vie) ? $duree_de_vie : 3600*24; } } } private function initialiserOptionsParConfig() { while (list($nom, $valeur) = each($this->options)) { if (Config::existe($nom)) { $this->$nom = Config::get($nom); } } } /** * Teste si le cache est disponible pour l'id donné et (si oui) le retourne (sinon renvoie false) * * @param string $id l'identifiant du Cache. * @return string|false les données en cache. */ public function charger($id) { $contenu = false; if ($this->mise_en_cache) { $chemin_fichier_cache = $this->stockage_chemin.DS.$id.'.txt'; if (file_exists($chemin_fichier_cache ) && (time() - @filemtime($chemin_fichier_cache) < $this->duree_de_vie)) { $contenu = file_get_contents($chemin_fichier_cache); } } return $contenu; } /** * Sauvegarde la chaine de données dans un fichier texte. * * Note : $contenu est toujours de type "string". C'est à vous de gérer la sérialisation. * * @param string $contenu les données à mettre en cache. * @param string $id l'identifiant du Cache. * @return boolean true si aucun problème */ public function sauver($contenu, $id) { if (! $this->mise_en_cache) return FALSE; $ok = false; $chemin_fichier_cache = $this->stockage_chemin.DS.$id.'.txt'; if (!file_exists($chemin_fichier_cache) || (time() - @filemtime($chemin_fichier_cache) > $this->duree_de_vie)) { $dossier_fichier_cache = dirname($chemin_fichier_cache); if (!is_dir($dossier_fichier_cache)) { mkdir($dossier_fichier_cache, 0755, true); } $fh = fopen($chemin_fichier_cache,'w+'); if ($fh) { if (fwrite($fh, $contenu)) { if (fclose($fh)) { error_log(sprintf("%s: caching \"%s\" [%s]", __FILE__, $chemin_fichier_cache, @$_SERVER['REQUEST_URI'])); $ok = true; } } // Voir #ZF-4422 pour la raison de l'utilisation de octdec() @chmod($chemin_fichier_cache, octdec('0777')); } } if(!$ok) error_log(sprintf("%s: ERROR trying to cache \"%s\" [%s]", __FILE__, $chemin_fichier_cache, @$_SERVER['REQUEST_URI'])); return $ok; } }