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