Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 693 | Blame | 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($ressources, $parametres) {
                $id = $this->genererID($ressources, $parametres);
                $retour = unserialize($this->cache->charger($id));
                if ($retour == false) {
                        $retour = $this->mettreEnCache($ressources, $parametres);
                }

                return $retour;
        }

        public function mettreEnCache($ressources, $parametres) {
                $retour = $this->service->consulter($ressources, $parametres);
                $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;
        }
}
?>