Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 880 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
class CacheEflore {

        private $service;
        private $config;
        private $dureecache = 0;
        private $projetNom;
        private $serviceNom;
        private $cache;
        private $cacheActif;

        public function __construct($service, $projetNom, $serviceNom, $cacheActif) {
                $this->cacheActif = $cacheActif;
                $this->service = $service;
                $this->chargerDureeCache();
                $this->projetNom = $projetNom;
                $this->serviceNom = $serviceNom;
                $this->cache = new CacheSimple(array(
                        "mise_en_cache" => true, 
                        "stockage_chemin" => Config::get('chemin_cache').'services'.DS, 
                        "duree_de_vie" => $this->dureecache));
        }

        public function chargerDureeCache() {
                if ($this->cacheActif == "1") {
                        $this->dureecache = Commun::getDureeCache();
                }
        }

        public function consulter() {
                list($ressources, $parametres) = func_get_args();
                $args = func_get_args(); // XXX: PHP-5.3
                $id = $this->genererID($ressources, $parametres);
                $retour = unserialize($this->cache->charger($id));
                if ($retour == false) {
                        $retour = call_user_func_array(array($this, 'mettreEnCache'),
                                                                                   $args);
                }

                return $retour;
        }

        public function mettreEnCache() {
                list($ressources, $parametres) = func_get_args();
                $args = func_get_args(); // XXX: PHP-5.3
                $retour = call_user_func_array(array($this->service, 'consulter'),
                                                                           $args);
                $id = $this->genererID($ressources, $parametres);
                if ($this->dureecache > 0) {
                        $this->cache->sauver(serialize($retour), $id);
                }
                return $retour;
        }

        public function genererID($ressources, $parametres) {
                $chaineRessources = "";
                $chaineParametres = "";
                if (count($ressources) > 0) {
                        foreach ($ressources as $key => $val) {
                                $chaineRessources .= "$key:$val;";
                        }
                }

                if (count($parametres) > 0) {
                        foreach ($parametres as $key => $val) {
                                $chaineParametres .= "$key:$val;";
                        }
                }

                $chaineMD5 = $this->projetNom.'/'.$this->serviceNom.'/'.md5($chaineRessources.$chaineParametres);
                return $chaineMD5;
        }
}
?>