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