Subversion Repositories Applications.framework

Rev

Rev 249 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
249 jpm 1
<?php
2
class ServiceCache extends Service {
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
65
		$meta01 = $cache->getMetadonnees($id_test);
66
		echo 'Métadonnées du cache : <pre>'.print_r($meta01, true).'</pre>';
67
 
68
		// Nettoyage & suppression
69
		echo "Temps avant suppression du cache : ".(time() - $meta01['expiration']).'s<br/>';
70
		if ((time() - $meta01['expiration']) == 0) {
71
			// Suppressions
72
			$cache->supprimer($id_test);
73
			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/>';
74
 
75
			// Nettoyage
76
			$cache->nettoyer(Cache::NETTOYAGE_MODE_AVEC_LES_TAGS, array($tag02, $tag03));
77
			echo (!$cache->tester($id03) ? "OK : le cache '$id03' a bien été supprimé" : "KO : le cache '$id03' n'a pas été supprimé").'<br/>';
78
			$cache->nettoyer(Cache::NETTOYAGE_MODE_SANS_LES_TAGS, array($tag01));
79
			echo ($cache->tester($id02) ? "OK : le cache '$id02' n'a pas été supprimé" : "KO : le cache '$id02' a été supprimé").'<br/>';
80
			$cache->nettoyer(Cache::NETTOYAGE_MODE_EXPIRATION);
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_TOUS);
83
			echo (!$cache->tester($id01) ? "OK : le cache '$id02' a été supprimé" : "KO : le cache '$id02' n'a pas été supprimé").'<br/>';
84
		} else if ((time() - $meta01['expiration']) < -60) {
85
			// Test ajout d'une durée de vie supplémentaire
86
			$meta01 = $cache->getMetadonnees($id_test);
87
			echo "Durée de vie du cache '$id_test' : ".$meta01['expiration'].'<br/>';
88
			$cache->ajouterSupplementDureeDeVie($id_test, -3540);
89
			$meta02 = $cache->getMetadonnees($id_test);
90
			echo "Durée de vie du cache '$id_test' après ajout supplément de -3540s : ".$meta02['expiration'].
91
				' - diff. : '.($meta02['expiration'] - $meta01['expiration']).'<br/>';
92
		}
93
	}
94
 
95
	/**
96
     * Determine system TMP directory and detect if we have read access
97
     *
98
     * inspired from Zend_File_Transfer_Adapter_Abstract & Zend_Cache
99
     *
100
     * @return string
101
     */
102
    public function getTmpDir() {
103
        $tmpdir = array();
104
        foreach (array($_ENV, $_SERVER) as $tab) {
105
            foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
106
                if (isset($tab[$key])) {
107
                    if (($key == 'windir') or ($key == 'SystemRoot')) {
108
                        $dir = realpath($tab[$key] . '\\temp');
109
                    } else {
110
                        $dir = realpath($tab[$key]);
111
                    }
112
                    if ($this->isGoodTmpDir($dir)) {
113
                        return $dir;
114
                    }
115
                }
116
            }
117
        }
118
        $upload = ini_get('upload_tmp_dir');
119
        if ($upload) {
120
            $dir = realpath($upload);
121
            if ($this->isGoodTmpDir($dir)) {
122
                return $dir;
123
            }
124
        }
125
        if (function_exists('sys_get_temp_dir')) {
126
            $dir = sys_get_temp_dir();
127
            if ($this->isGoodTmpDir($dir)) {
128
                return $dir;
129
            }
130
        }
131
        // Attemp to detect by creating a temporary file
132
        $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
133
        if ($tempFile) {
134
            $dir = realpath(dirname($tempFile));
135
            unlink($tempFile);
136
            if ($this->isGoodTmpDir($dir)) {
137
                return $dir;
138
            }
139
        }
140
        if ($this->isGoodTmpDir('/tmp')) {
141
            return '/tmp';
142
        }
143
        if ($this->isGoodTmpDir('\\temp')) {
144
            return '\\temp';
145
        }
146
    }
147
 
148
    /**
149
     * Verify if the given temporary directory is readable and writable
150
     *
151
     * @param $dir temporary directory
152
     * @return boolean true if the directory is ok
153
     */
154
    protected function isGoodTmpDir($dir){
155
        if (is_readable($dir)) {
156
            if (is_writable($dir)) {
157
                return true;
158
            }
159
        }
160
        return false;
161
    }
162
 
163
}
164
?>