Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 564 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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