Subversion Repositories eFlore/Applications.moissonnage

Rev

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

<?php
class CacheMoissonnage {
        
        private $service;
        private $config;
        private $dureecache = 0;
        private $serviceNom;
        private $cache;
        private $cacheActif;
        
        public function __construct($service, $serviceNom, $cacheActif) {
                $this->cacheActif = $cacheActif;
                $this->service = $service;
                $this->chargerDureeCache();
                $this->serviceNom = $serviceNom;
                $this->cache = new CacheSimple(array(
                        "mise_en_cache"   => true,
                        "stockage_chemin" => Config::get("chemincache"),
                        "duree_de_vie"    => $this->dureecache
                ));
        }

        public function chargerDureeCache() {
                if ($this->cacheActif == "1") {
                        $this->dureecache = $this->service->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->serviceNom.'/'.md5($chaineRessources.$chaineParametres);
                return $chaineMD5;
        }
}


?>