Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 801 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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