Subversion Repositories eFlore/Projets.eflore-projets

Rev

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