249 |
jpm |
1 |
<?php
|
270 |
jpm |
2 |
class ServiceCacheFichier extends Service {
|
249 |
jpm |
3 |
|
|
|
4 |
public function consulter($ressources, $parametres) {
|
|
|
5 |
$options = array(
|
|
|
6 |
'stockage_mode' => Cache::STOCKAGE_MODE_FICHIER,
|
|
|
7 |
'stockage_chemin' => $this->getTmpDir(),
|
|
|
8 |
'controle_ecriture' => false,
|
|
|
9 |
'mise_en_cache' => true,
|
|
|
10 |
'cache_id_prefixe' => null,
|
|
|
11 |
'serialisation_auto' => false,
|
|
|
12 |
'nettoyage_auto' => 10,
|
|
|
13 |
'duree_de_vie' => 3600,
|
|
|
14 |
);
|
|
|
15 |
$options_stockage = array(
|
|
|
16 |
'fichier_verrou' => false,
|
|
|
17 |
'controle_lecture' => false,
|
|
|
18 |
'controle_lecture_type' => 'crc32',
|
|
|
19 |
'dossier_niveau' => 2,
|
|
|
20 |
'dossier_umask' => 0707,
|
|
|
21 |
'fichier_prefixe' => 'tbf',
|
|
|
22 |
'fichier_umask' => 0606,
|
|
|
23 |
'metadonnees_max_taille' => 100
|
|
|
24 |
);
|
|
|
25 |
$cache = new Cache($options, $options_stockage);
|
|
|
26 |
|
|
|
27 |
$id = 'monCache'; // id de cache de "ce que l'on veut cacher"
|
|
|
28 |
$id01 = $id.'01';
|
|
|
29 |
$id02 = $id.'02';
|
|
|
30 |
$id03 = $id.'03';
|
|
|
31 |
if (!($donnees = $cache->charger($id01))) {
|
|
|
32 |
$donnees = 'Mise en cache à : '.strftime('%A %d %B %Y à %H:%M:%S', time()).'<br/>';
|
|
|
33 |
for ($i = 0; $i < 100000; $i++) {
|
|
|
34 |
$donnees .= $i;
|
|
|
35 |
}
|
|
|
36 |
$cache->sauver($donnees, $id01);
|
|
|
37 |
$cache->sauver($donnees, $id02, array('tagTest01'));
|
|
|
38 |
$cache->sauver($donnees, $id03, array('tagTest01','tagTest02', 'tagTest03'));
|
|
|
39 |
}
|
|
|
40 |
// Affichage des données
|
|
|
41 |
echo $donnees.'<br/>';
|
|
|
42 |
|
|
|
43 |
// Affichage des ids du cache
|
|
|
44 |
echo 'Ids des enregistrements en cache : <pre>'.print_r($cache->getIds(), true).'</pre>';
|
|
|
45 |
|
|
|
46 |
// Affichage des ids du cache
|
|
|
47 |
echo 'Tags des enregistrements en cache : <pre>'.print_r($cache->getTags(), true).'</pre>';
|
|
|
48 |
|
|
|
49 |
// Affichage des ids du cache pour un tag donnée
|
|
|
50 |
$tag01 = 'tagTest01';
|
|
|
51 |
$tag02 = 'tagTest02';
|
|
|
52 |
$tag03 = 'tagTest03';
|
|
|
53 |
echo "Ids avec les tag '$tag02, $tag03' : ".'<pre>'.print_r($cache->getIdsAvecLesTags(array($tag02, $tag03)), true).'</pre>';
|
|
|
54 |
echo "Ids possédant le tag '$tag01' : ".'<pre>'.print_r($cache->getIdsAvecUnTag(array($tag01)), true).'</pre>';
|
|
|
55 |
echo "Ids sans les tag '$tag01, $tag02' : ".'<pre>'.print_r($cache->getIdsSansLesTags(array($tag01, $tag02)), true).'</pre>';
|
|
|
56 |
|
|
|
57 |
// Test du test de l'existence du cache
|
|
|
58 |
$id_test = $id01;
|
|
|
59 |
echo ($cache->tester($id_test) ? "OK : le cache '$id_test' existe" : "KO : le cache '$id_test' devrait exister").'<br/>';
|
|
|
60 |
|
|
|
61 |
// Affichage du pourcentage de remplissage
|
|
|
62 |
echo 'Pourcentage de remplissage : '.$cache->getPourcentageRemplissage().'%<br/>';
|
|
|
63 |
|
|
|
64 |
// Affichage des métadonnées
|
270 |
jpm |
65 |
$meta03 = $cache->getMetadonnees($id03);
|
|
|
66 |
echo "Métadonnées du cache '$id03' : <pre>".print_r($meta03, true).'</pre>';
|
249 |
jpm |
67 |
$meta01 = $cache->getMetadonnees($id_test);
|
270 |
jpm |
68 |
echo "Métadonnées du cache '$id_test' : <pre>".print_r($meta01, true).'</pre>';
|
249 |
jpm |
69 |
|
|
|
70 |
// Nettoyage & suppression
|
|
|
71 |
echo "Temps avant suppression du cache : ".(time() - $meta01['expiration']).'s<br/>';
|
270 |
jpm |
72 |
if ((time() - $meta01['expiration']) >= -30) {
|
249 |
jpm |
73 |
// Suppressions
|
|
|
74 |
$cache->supprimer($id_test);
|
|
|
75 |
echo (!$cache->tester($id_test) ? "OK : le cache '$id_test' a bien été supprimé" : "KO : le cache '$id_test' n'a pas été supprimé").'<br/>';
|
|
|
76 |
|
|
|
77 |
// Nettoyage
|
|
|
78 |
$cache->nettoyer(Cache::NETTOYAGE_MODE_AVEC_LES_TAGS, array($tag02, $tag03));
|
|
|
79 |
echo (!$cache->tester($id03) ? "OK : le cache '$id03' a bien été supprimé" : "KO : le cache '$id03' n'a pas été supprimé").'<br/>';
|
|
|
80 |
$cache->nettoyer(Cache::NETTOYAGE_MODE_SANS_LES_TAGS, array($tag01));
|
|
|
81 |
echo ($cache->tester($id02) ? "OK : le cache '$id02' n'a pas été supprimé" : "KO : le cache '$id02' a été supprimé").'<br/>';
|
|
|
82 |
$cache->nettoyer(Cache::NETTOYAGE_MODE_EXPIRATION);
|
|
|
83 |
echo ($cache->tester($id02) ? "OK : le cache '$id02' n'a pas été supprimé" : "KO : le cache '$id02' a été supprimé").'<br/>';
|
|
|
84 |
$cache->nettoyer(Cache::NETTOYAGE_MODE_TOUS);
|
|
|
85 |
echo (!$cache->tester($id01) ? "OK : le cache '$id02' a été supprimé" : "KO : le cache '$id02' n'a pas été supprimé").'<br/>';
|
|
|
86 |
} else if ((time() - $meta01['expiration']) < -60) {
|
|
|
87 |
// Test ajout d'une durée de vie supplémentaire
|
|
|
88 |
$meta01 = $cache->getMetadonnees($id_test);
|
|
|
89 |
echo "Durée de vie du cache '$id_test' : ".$meta01['expiration'].'<br/>';
|
|
|
90 |
$cache->ajouterSupplementDureeDeVie($id_test, -3540);
|
|
|
91 |
$meta02 = $cache->getMetadonnees($id_test);
|
|
|
92 |
echo "Durée de vie du cache '$id_test' après ajout supplément de -3540s : ".$meta02['expiration'].
|
|
|
93 |
' - diff. : '.($meta02['expiration'] - $meta01['expiration']).'<br/>';
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Determine system TMP directory and detect if we have read access
|
|
|
99 |
*
|
|
|
100 |
* inspired from Zend_File_Transfer_Adapter_Abstract & Zend_Cache
|
|
|
101 |
*
|
|
|
102 |
* @return string
|
|
|
103 |
*/
|
|
|
104 |
public function getTmpDir() {
|
|
|
105 |
$tmpdir = array();
|
|
|
106 |
foreach (array($_ENV, $_SERVER) as $tab) {
|
|
|
107 |
foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
|
|
|
108 |
if (isset($tab[$key])) {
|
|
|
109 |
if (($key == 'windir') or ($key == 'SystemRoot')) {
|
|
|
110 |
$dir = realpath($tab[$key] . '\\temp');
|
|
|
111 |
} else {
|
|
|
112 |
$dir = realpath($tab[$key]);
|
|
|
113 |
}
|
|
|
114 |
if ($this->isGoodTmpDir($dir)) {
|
|
|
115 |
return $dir;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
$upload = ini_get('upload_tmp_dir');
|
|
|
121 |
if ($upload) {
|
|
|
122 |
$dir = realpath($upload);
|
|
|
123 |
if ($this->isGoodTmpDir($dir)) {
|
|
|
124 |
return $dir;
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
if (function_exists('sys_get_temp_dir')) {
|
|
|
128 |
$dir = sys_get_temp_dir();
|
|
|
129 |
if ($this->isGoodTmpDir($dir)) {
|
|
|
130 |
return $dir;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
// Attemp to detect by creating a temporary file
|
|
|
134 |
$tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
|
|
|
135 |
if ($tempFile) {
|
|
|
136 |
$dir = realpath(dirname($tempFile));
|
|
|
137 |
unlink($tempFile);
|
|
|
138 |
if ($this->isGoodTmpDir($dir)) {
|
|
|
139 |
return $dir;
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
if ($this->isGoodTmpDir('/tmp')) {
|
|
|
143 |
return '/tmp';
|
|
|
144 |
}
|
|
|
145 |
if ($this->isGoodTmpDir('\\temp')) {
|
|
|
146 |
return '\\temp';
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Verify if the given temporary directory is readable and writable
|
|
|
152 |
*
|
|
|
153 |
* @param $dir temporary directory
|
|
|
154 |
* @return boolean true if the directory is ok
|
|
|
155 |
*/
|
|
|
156 |
protected function isGoodTmpDir($dir){
|
|
|
157 |
if (is_readable($dir)) {
|
|
|
158 |
if (is_writable($dir)) {
|
|
|
159 |
return true;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
return false;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
}
|
|
|
166 |
?>
|