Subversion Repositories eFlore/Projets.eflore-projets

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
536 gduche 1
<?php
2
class CacheEflore {
670 jpm 3
 
536 gduche 4
	private $service;
5
	private $config;
6
	private $dureecache = 0;
7
	private $projetNom;
8
	private $serviceNom;
9
	private $cache;
10
	private $cacheActif;
670 jpm 11
 
536 gduche 12
	public function __construct($service, $projetNom, $serviceNom, $cacheActif) {
13
		$this->cacheActif = $cacheActif;
14
		$this->service = $service;
15
		$this->chargerDureeCache();
16
		$this->projetNom = $projetNom;
17
		$this->serviceNom = $serviceNom;
693 isa 18
		$this->cache = new CacheSimple(array(
19
			"mise_en_cache" => true,
20
			"stockage_chemin" => Config::get('chemin_cache').'services'.DS,
21
			"duree_de_vie" => $this->dureecache));
536 gduche 22
	}
23
 
24
	public function chargerDureeCache() {
25
		if ($this->cacheActif == "1") {
801 raphael 26
			$this->dureecache = Commun::getDureeCache();
536 gduche 27
		}
28
	}
670 jpm 29
 
536 gduche 30
	public function consulter($ressources, $parametres) {
31
		$id = $this->genererID($ressources, $parametres);
32
		$retour = unserialize($this->cache->charger($id));
33
		if ($retour == false) {
34
			$retour = $this->mettreEnCache($ressources, $parametres);
35
		}
670 jpm 36
 
536 gduche 37
		return $retour;
38
	}
670 jpm 39
 
536 gduche 40
	public function mettreEnCache($ressources, $parametres) {
41
		$retour = $this->service->consulter($ressources, $parametres);
42
		$id = $this->genererID($ressources, $parametres);
43
		if ($this->dureecache > 0) {
44
			$this->cache->sauver(serialize($retour), $id);
45
		}
46
		return $retour;
47
	}
670 jpm 48
 
536 gduche 49
	public function genererID($ressources, $parametres) {
50
		$chaineRessources = "";
51
		$chaineParametres = "";
52
		if (count($ressources) > 0) {
53
			foreach ($ressources as $key => $val) {
54
				$chaineRessources .= "$key:$val;";
55
			}
56
		}
670 jpm 57
 
536 gduche 58
		if (count($parametres) > 0) {
59
			foreach ($parametres as $key => $val) {
60
				$chaineParametres .= "$key:$val;";
61
			}
62
		}
670 jpm 63
 
616 aurelien 64
		$chaineMD5 = $this->projetNom.'/'.$this->serviceNom.'/'.md5($chaineRessources.$chaineParametres);
536 gduche 65
		return $chaineMD5;
66
	}
67
}
68
?>