Subversion Repositories eFlore/Applications.moissonnage

Rev

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

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