1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: class CacheSimple {
24:
25: private $mise_en_cache = null;
26: private $stockage_chemin = null;
27: private $duree_de_vie = null;
28:
29: private $options = array(
30: 'mise_en_cache' => 'true',
31: 'stockage_chemin' => 'Fichier::getDossierTmp()',
32: 'duree_de_vie' => '3600*24'
33: );
34:
35: public function __construct($options = array()) {
36: extract($options);
37: $this->mise_en_cache = is_bool($mise_en_cache) ? $mise_en_cache : $mise_en_cache == '' ? false : true;
38:
39: if ($this->mise_en_cache) {
40: $this->stockage_chemin = isset($stockage_chemin) ? realpath($stockage_chemin) : Fichier::getDossierTmp();
41: if (!realpath($stockage_chemin)) {
42: error_log(sprintf("%s: Attention, %s invalide: creation [%s]",
43: __FILE__,
44: $stockage_chemin,
45: @$_SERVER['REQUEST_URI']));
46: mkdir($stockage_chemin, 0755, TRUE);
47: }
48: if (!realpath($stockage_chemin)) {
49: error_log(sprintf("%s: Attention, realpath(%s) invalide [%s]",
50: __FILE__,
51: $stockage_chemin,
52: @$_SERVER['REQUEST_URI']));
53: } else if(!is_writable(realpath($stockage_chemin))) {
54: error_log(sprintf("%s: Attention, realpath(%s) non-inscriptible [%s]",
55: __FILE__,
56: realpath($stockage_chemin),
57: @$_SERVER['REQUEST_URI']));
58: } else {
59: $this->duree_de_vie = isset($duree_de_vie) ? $duree_de_vie : 3600*24;
60: }
61: }
62: }
63:
64: private function initialiserOptionsParConfig() {
65: while (list($nom, $valeur) = each($this->options)) {
66: if (Config::existe($nom)) {
67: $this->$nom = Config::get($nom);
68: }
69: }
70: }
71:
72: 73: 74: 75: 76: 77:
78: public function charger($id) {
79: $contenu = false;
80: if ($this->mise_en_cache) {
81: $chemin_fichier_cache = $this->stockage_chemin.DS.$id.'.txt';
82: if (file_exists($chemin_fichier_cache ) && (time() - @filemtime($chemin_fichier_cache) < $this->duree_de_vie)) {
83: $contenu = file_get_contents($chemin_fichier_cache);
84: }
85: }
86: return $contenu;
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97:
98: public function sauver($contenu, $id) {
99: if (! $this->mise_en_cache) return FALSE;
100:
101: $ok = false;
102: $chemin_fichier_cache = $this->stockage_chemin.DS.$id.'.txt';
103: if (!file_exists($chemin_fichier_cache) || (time() - @filemtime($chemin_fichier_cache) > $this->duree_de_vie)) {
104: $dossier_fichier_cache = dirname($chemin_fichier_cache);
105: if (!is_dir($dossier_fichier_cache))
106: {
107: mkdir($dossier_fichier_cache, 0755, true);
108: }
109:
110: $fh = fopen($chemin_fichier_cache,'w+');
111: if ($fh) {
112: if (fwrite($fh, $contenu)) {
113: if (fclose($fh)) {
114: error_log(sprintf("%s: caching \"%s\" [%s]", __FILE__, $chemin_fichier_cache, @$_SERVER['REQUEST_URI']));
115:
116: $ok = true;
117: }
118: }
119:
120: @chmod($chemin_fichier_cache, octdec('0777'));
121: }
122: }
123: if(!$ok) error_log(sprintf("%s: ERROR trying to cache \"%s\" [%s]", __FILE__, $chemin_fichier_cache, @$_SERVER['REQUEST_URI']));
124: return $ok;
125: }
126: }
127: