Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 801 | 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;
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
 
851 raphael 30
	public function consulter() {
31
		list($ressources, $parametres) = func_get_args();
536 gduche 32
		$id = $this->genererID($ressources, $parametres);
33
		$retour = unserialize($this->cache->charger($id));
34
		if ($retour == false) {
851 raphael 35
			$retour = call_user_func_array(array($this, 'mettreEnCache'),
36
										   func_get_args());
536 gduche 37
		}
670 jpm 38
 
536 gduche 39
		return $retour;
40
	}
670 jpm 41
 
851 raphael 42
	public function mettreEnCache() {
43
		list($ressources, $parametres) = func_get_args();
44
		$retour = call_user_func_array(array($this->service, 'consulter'),
45
									   func_get_args());
536 gduche 46
		$id = $this->genererID($ressources, $parametres);
47
		if ($this->dureecache > 0) {
48
			$this->cache->sauver(serialize($retour), $id);
49
		}
50
		return $retour;
51
	}
670 jpm 52
 
536 gduche 53
	public function genererID($ressources, $parametres) {
54
		$chaineRessources = "";
55
		$chaineParametres = "";
56
		if (count($ressources) > 0) {
57
			foreach ($ressources as $key => $val) {
58
				$chaineRessources .= "$key:$val;";
59
			}
60
		}
670 jpm 61
 
536 gduche 62
		if (count($parametres) > 0) {
63
			foreach ($parametres as $key => $val) {
64
				$chaineParametres .= "$key:$val;";
65
			}
66
		}
670 jpm 67
 
616 aurelien 68
		$chaineMD5 = $this->projetNom.'/'.$this->serviceNom.'/'.md5($chaineRessources.$chaineParametres);
536 gduche 69
		return $chaineMD5;
70
	}
71
}
72
?>