Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 616 | 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 {
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;
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
	}
670 jpm 26
 
536 gduche 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
		}
670 jpm 33
 
536 gduche 34
		return $retour;
35
	}
670 jpm 36
 
536 gduche 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
	}
670 jpm 45
 
536 gduche 46
	public function genererID($ressources, $parametres) {
47
		$chaineRessources = "";
48
		$chaineParametres = "";
49
		if (count($ressources) > 0) {
50
			foreach ($ressources as $key => $val) {
51
				$chaineRessources .= "$key:$val;";
52
			}
53
		}
670 jpm 54
 
536 gduche 55
		if (count($parametres) > 0) {
56
			foreach ($parametres as $key => $val) {
57
				$chaineParametres .= "$key:$val;";
58
			}
59
		}
670 jpm 60
 
616 aurelien 61
		$chaineMD5 = $this->projetNom.'/'.$this->serviceNom.'/'.md5($chaineRessources.$chaineParametres);
536 gduche 62
		return $chaineMD5;
63
	}
64
}
65
?>