[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /** 3 * Fast, light and safe Cache Class 4 * 5 * CacheLite is a fast, light and safe cache system. It's optimized 6 * for file containers. It is fast and safe (because it uses file 7 * locking and/or anti-corruption tests). 8 * 9 * This a simplified version of the Cache_Lite class available in PEAR 10 * 11 * Based on version $ Id: Lite.php,v 1.11 2003/02/23 16:18:53 fab Exp $ of Cache_Lite 12 * Original author Fabien MARTY <fab@php.net> 13 * 14 */ 15 16 class CacheLite 17 { 18 19 // --- Private properties --- 20 21 /** 22 * Directory where to put the cache files 23 * (make sure to add a trailing slash) 24 * 25 * @var string $_cacheDir 26 */ 27 var $_cacheDir = '/tmp/'; 28 29 /** 30 * Enable / disable caching 31 * 32 * (can be very usefull for the debug of cached scripts) 33 * 34 * @var boolean $_caching 35 */ 36 var $_caching = true; 37 38 /** 39 * Enable / disable fileLocking 40 * 41 * (can avoid cache corruption under bad circumstances) 42 * 43 * @var boolean $_fileLocking 44 */ 45 var $_fileLocking = true; 46 47 /** 48 * Timestamp of the last valid cache 49 * 50 * @var int $_refreshTime 51 */ 52 var $_refreshTime; 53 54 /** 55 * File name (with path) 56 * 57 * @var string $_file 58 */ 59 var $_file; 60 61 /** 62 * Enable / disable write control (the cache is read just after writing to detect corrupt entries) 63 * 64 * Enable write control will lightly slow the cache writing but not the cache reading 65 * Write control can detect some corrupt cache files but maybe it's not a perfect control 66 * 67 * @var boolean $_writeControl 68 */ 69 var $_writeControl = true; 70 71 /** 72 * Enable / disable read control 73 * 74 * If enabled, a control key is embeded in cache file and this key is compared with the one 75 * calculated after the reading. 76 * 77 * @var boolean $_writeControl 78 */ 79 var $_readControl = false; 80 81 /** 82 * Current cache id 83 * 84 * @var string $_id 85 */ 86 var $_id; 87 88 // --- Public methods --- 89 90 /** 91 * Constructor 92 * 93 * $options is an assoc. Available options are : 94 * $options = array( 95 * 'cacheDir' => directory where to put the cache files (string), 96 * 'caching' => enable / disable caching (boolean), 97 * 'lifeTime' => cache lifetime in seconds (int), 98 * 'fileLocking' => enable / disable fileLocking (boolean), 99 * 'writeControl' => enable / disable write control (boolean), 100 * 'readControl' => enable / disable read control (boolean), 101 * ); 102 * 103 * @param array $options options 104 * @access public 105 */ 106 function CacheLite($options = array(NULL)) 107 { 108 $availableOptions = '{cacheDir}{caching}{lifeTime}{fileLocking}{writeControl}{readControl}'; 109 while (list($key, $value) = each($options)) { 110 if (strpos('>'.$availableOptions, '{'.$key.'}')) { 111 $property = '_'.$key; 112 $this->$property = $value; 113 } 114 } 115 $this->_refreshTime = time() - $this->_lifeTime; 116 } 117 118 /** 119 * Test if a cache is available and (if yes) return it 120 * 121 * @param string $id cache id 122 * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested 123 * @return string data of the cache (or false if no cache available) 124 * @access public 125 */ 126 function get($id, $doNotTestCacheValidity = false) 127 { 128 $this->_id = $id; 129 $data = false; 130 if ($this->_caching) { 131 $this->_setFileName($id); 132 if ($doNotTestCacheValidity) { 133 if (file_exists($this->_file)) { 134 $data = $this->_read(); 135 } 136 } else { 137 if (@filemtime($this->_file) > $this->_refreshTime) { 138 $data = $this->_read(); 139 } 140 } 141 return $data; 142 } 143 return false; 144 } 145 146 /** 147 * Save some data in a cache file 148 * 149 * @param string $data data to put in cache 150 * @param string $id cache id 151 * @return boolean true if no problem 152 * @access public 153 */ 154 function save($data, $id = NULL) 155 { 156 if ($this->_caching) { 157 if (isset($id)) { 158 $this->_setFileName($id); 159 } 160 if ($this->_writeControl) { 161 if (!$this->_writeAndControl($data)) { 162 @touch($this->_file, time() - 2*abs($this->_lifeTime)); 163 return false; 164 } else { 165 return true; 166 } 167 } else { 168 return $this->_write($data); 169 } 170 } 171 return false; 172 } 173 174 /** 175 * Remove a cache file 176 * 177 * @param string $id cache id 178 * @return boolean true if no problem 179 * @access public 180 */ 181 function remove($id) 182 { 183 $this->_setFileName($id); 184 return @unlink($this->_file); 185 } 186 187 /** 188 * Clean the cache 189 * 190 * if no group is specified all cache files will be destroyed 191 * else only cache files of the specified group will be destroyed 192 * 193 * @param string $group name of the cache group 194 * @return boolean true if no problem 195 * @access public 196 */ 197 function clean() 198 { 199 $motif = 'cache_'; 200 if (!($dh = opendir($this->_cacheDir))) { 201 return false; 202 } 203 while ($file = readdir($dh)) { 204 if (($file != '.') && ($file != '..')) { 205 $file = $this->_cacheDir . $file; 206 if (is_file($file)) { 207 if (strpos($file, $motif, 0)) { 208 if (!@unlink($file)) { 209 return false; 210 } 211 } 212 } 213 } 214 } 215 return true; 216 } 217 218 /** 219 * Set a new life time 220 * 221 * @param int $newLifeTime new life time (in seconds) 222 * @access public 223 */ 224 function setLifeTime($newLifeTime) 225 { 226 $this->_lifeTime = $newLifeTime; 227 $this->_refreshTime = time() - $newLifeTime; 228 } 229 230 231 // --- Private methods --- 232 233 /** 234 * Make a file name (with path) 235 * 236 * @param string $id cache id 237 * @access private 238 */ 239 function _setFileName($id) 240 { 241 $this->_file = $this->_cacheDir.'cache_'.$id; 242 } 243 244 /** 245 * Read the cache file and return the content 246 * 247 * @return string content of the cache file 248 * @access private 249 */ 250 function _read() 251 { 252 $fp = @fopen($this->_file, "r"); 253 if ($this->_fileLocking) @flock($fp, LOCK_SH); 254 if ($fp) { 255 clearstatcache(); // because the filesize can be cached by PHP itself... 256 $length = @filesize($this->_file); 257 $mqr = get_magic_quotes_runtime(); 258 set_magic_quotes_runtime(0); 259 if ($this->_readControl) { 260 $hashControl = @fread($fp, 32); 261 $length = $length - 32; 262 } 263 $data = @fread($fp, $length); 264 set_magic_quotes_runtime($mqr); 265 if ($this->_fileLocking) @flock($fp, LOCK_UN); 266 @fclose($fp); 267 if ($this->_readControl) { 268 $hashData = $this->_hash($data, $this->_readControlType); 269 if ($hashData != $hashControl) { 270 @touch($this->_file, time() - 2*abs($this->_lifeTime)); 271 return false; 272 } 273 } 274 return $data; 275 } 276 return false; 277 } 278 279 /** 280 * Write the given data in the cache file 281 * 282 * @param string $data data to put in cache 283 * @return boolean true if ok 284 * @access private 285 */ 286 function _write($data) 287 { 288 $fp = @fopen($this->_file, "w"); 289 if ($fp) { 290 if ($this->_fileLocking) @flock($fp, LOCK_EX); 291 if ($this->_readControl) { 292 @fwrite($fp, $this->_hash($data, $this->_readControlType), 32); 293 } 294 $len = strlen($data); 295 @fwrite($fp, $data, $len); 296 if ($this->_fileLocking) @flock($fp, LOCK_UN); 297 @fclose($fp); 298 return true; 299 } 300 return false; 301 } 302 303 /** 304 * Write the given data in the cache file and control it just after to avoir corrupted cache entries 305 * 306 * @param string $data data to put in cache 307 * @return boolean true if the test is ok 308 * @access private 309 */ 310 function _writeAndControl($data) 311 { 312 $this->_write($data); 313 $dataRead = $this->_read($data); 314 return ($dataRead==$data); 315 } 316 317 /** 318 * Make a control key with the string containing datas 319 * 320 * @param string $data data 321 * @return string control key 322 * @access private 323 */ 324 function _hash($data, $controlType) 325 { 326 return sprintf('% 32d', crc32($data)); 327 } 328 329 } 330 331 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |