Subversion Repositories Applications.framework

Rev

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