[ Index ] |
PHP Cross Reference of phpBB 3.0 Beta 3 |
Search
|
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * @package acm 5 * @version $Id: acm_file.php,v 1.42 2006/10/07 17:40:06 acydburn Exp $ 6 * @copyright (c) 2005 phpBB Group 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 8 * 9 */ 10 11 /** 12 * ACM File Based Caching 13 * @package acm 14 */ 15 class acm 16 { 17 var $vars = array(); 18 var $var_expires = array(); 19 var $is_modified = false; 20 21 var $sql_rowset = array(); 22 var $sql_row_pointer = array(); 23 24 /** 25 * Set cache path 26 */ 27 function acm() 28 { 29 global $phpbb_root_path; 30 $this->cache_dir = $phpbb_root_path . 'cache/'; 31 } 32 33 /** 34 * Load global cache 35 */ 36 function load() 37 { 38 global $phpEx; 39 if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) 40 { 41 include($this->cache_dir . 'data_global.' . $phpEx); 42 } 43 else 44 { 45 return false; 46 } 47 48 return true; 49 } 50 51 /** 52 * Unload cache object 53 */ 54 function unload() 55 { 56 $this->save(); 57 unset($this->vars); 58 unset($this->var_expires); 59 unset($this->sql_rowset); 60 unset($this->sql_row_pointer); 61 } 62 63 /** 64 * Save modified objects 65 */ 66 function save() 67 { 68 if (!$this->is_modified) 69 { 70 return; 71 } 72 73 global $phpEx; 74 $file = "<?php\n\$this->vars = " . $this->format_array($this->vars) . ";\n\n\$this->var_expires = " . $this->format_array($this->var_expires) . "\n?>"; 75 76 if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb')) 77 { 78 @flock($fp, LOCK_EX); 79 fwrite($fp, $file); 80 @flock($fp, LOCK_UN); 81 fclose($fp); 82 } 83 else 84 { 85 // Now, this occurred how often? ... phew, just tell the user then... 86 if (!@is_writeable($this->cache_dir)) 87 { 88 trigger_error($this->cache_dir . ' is NOT writeable.', E_USER_ERROR); 89 } 90 91 trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR); 92 } 93 94 $this->is_modified = false; 95 } 96 97 /** 98 * Tidy cache 99 */ 100 function tidy() 101 { 102 global $phpEx; 103 104 $dir = opendir($this->cache_dir); 105 while (($entry = readdir($dir)) !== false) 106 { 107 if (!preg_match('/^(sql_|data_(?!global))/', $entry)) 108 { 109 continue; 110 } 111 112 $expired = true; 113 include($this->cache_dir . $entry); 114 if ($expired) 115 { 116 @unlink($this->cache_dir . $entry); 117 } 118 } 119 @closedir($dir); 120 121 if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) 122 { 123 if (!sizeof($this->vars)) 124 { 125 $this->load(); 126 } 127 128 foreach ($this->var_expires as $var_name => $expires) 129 { 130 if (time() > $expires) 131 { 132 $this->destroy($var_name); 133 } 134 } 135 } 136 137 set_config('cache_last_gc', time(), true); 138 } 139 140 /** 141 * Get saved cache object 142 */ 143 function get($var_name) 144 { 145 if ($var_name[0] == '_') 146 { 147 global $phpEx; 148 149 if (!$this->_exists($var_name)) 150 { 151 return false; 152 } 153 154 include($this->cache_dir . 'data' . $var_name . ".$phpEx"); 155 return (isset($data)) ? $data : false; 156 } 157 else 158 { 159 return ($this->_exists($var_name)) ? $this->vars[$var_name] : false; 160 } 161 } 162 163 /** 164 * Put data into cache 165 */ 166 function put($var_name, $var, $ttl = 31536000) 167 { 168 if ($var_name[0] == '_') 169 { 170 global $phpEx; 171 172 if ($fp = @fopen($this->cache_dir . 'data' . $var_name . ".$phpEx", 'wb')) 173 { 174 @flock($fp, LOCK_EX); 175 fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($var))) . "');\n?>"); 176 @flock($fp, LOCK_UN); 177 fclose($fp); 178 } 179 } 180 else 181 { 182 $this->vars[$var_name] = $var; 183 $this->var_expires[$var_name] = time() + $ttl; 184 $this->is_modified = true; 185 } 186 } 187 188 /** 189 * Purge cache data 190 */ 191 function purge() 192 { 193 // Purge all phpbb cache files 194 $dir = opendir($this->cache_dir); 195 while (($entry = readdir($dir)) !== false) 196 { 197 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) 198 { 199 continue; 200 } 201 202 @unlink($this->cache_dir . $entry); 203 } 204 @closedir($dir); 205 206 unset($this->vars); 207 unset($this->var_expires); 208 unset($this->sql_rowset); 209 unset($this->sql_row_pointer); 210 211 $this->is_modified = false; 212 } 213 214 /** 215 * Destroy cache data 216 */ 217 function destroy($var_name, $table = '') 218 { 219 global $phpEx; 220 221 if ($var_name == 'sql' && !empty($table)) 222 { 223 $regex = '(' . ((is_array($table)) ? implode('|', $table) : $table) . ')'; 224 225 $dir = opendir($this->cache_dir); 226 while (($entry = readdir($dir)) !== false) 227 { 228 if (strpos($entry, 'sql_') !== 0) 229 { 230 continue; 231 } 232 233 $fp = fopen($this->cache_dir . $entry, 'rb'); 234 $file = fread($fp, filesize($this->cache_dir . $entry)); 235 @fclose($fp); 236 237 if (preg_match('#/\*.*?\W' . $regex . '\W.*?\*/#s', $file, $m)) 238 { 239 @unlink($this->cache_dir . $entry); 240 } 241 } 242 @closedir($dir); 243 244 return; 245 } 246 247 if (!$this->_exists($var_name)) 248 { 249 return; 250 } 251 252 if ($var_name[0] == '_') 253 { 254 @unlink($this->cache_dir . 'data' . $var_name . ".$phpEx"); 255 } 256 else if (isset($this->vars[$var_name])) 257 { 258 $this->is_modified = true; 259 unset($this->vars[$var_name]); 260 unset($this->var_expires[$var_name]); 261 262 // We save here to let the following cache hits succeed 263 $this->save(); 264 } 265 } 266 267 /** 268 * Check if a given cache entry exist 269 */ 270 function _exists($var_name) 271 { 272 if ($var_name[0] == '_') 273 { 274 global $phpEx; 275 return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx"); 276 } 277 else 278 { 279 if (!sizeof($this->vars)) 280 { 281 $this->load(); 282 } 283 284 if (!isset($this->var_expires[$var_name])) 285 { 286 return false; 287 } 288 289 return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]); 290 } 291 } 292 293 /** 294 * Format an array to be stored on filesystem 295 */ 296 function format_array($array, $tab = '') 297 { 298 $tab .= "\t"; 299 300 $lines = array(); 301 foreach ($array as $k => $v) 302 { 303 if (is_array($v)) 304 { 305 $lines[] = "\n{$tab}'$k' => " . $this->format_array($v, $tab); 306 } 307 else if (is_int($v)) 308 { 309 $lines[] = "\n{$tab}'$k' => $v"; 310 } 311 else if (is_bool($v)) 312 { 313 $lines[] = "\n{$tab}'$k' => " . (($v) ? 'true' : 'false'); 314 } 315 else 316 { 317 $lines[] = "\n{$tab}'$k' => '" . str_replace("'", "\\'", str_replace('\\', '\\\\', $v)) . "'"; 318 } 319 } 320 321 return 'array(' . implode(',', $lines) . ')'; 322 } 323 324 /** 325 * Load cached sql query 326 */ 327 function sql_load($query) 328 { 329 global $phpEx; 330 331 // Remove extra spaces and tabs 332 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); 333 $query_id = sizeof($this->sql_rowset); 334 335 if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx")) 336 { 337 return false; 338 } 339 340 @include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"); 341 342 if (!isset($expired)) 343 { 344 return false; 345 } 346 else if ($expired) 347 { 348 @unlink($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"); 349 return false; 350 } 351 352 $this->sql_row_pointer[$query_id] = 0; 353 354 return $query_id; 355 } 356 357 /** 358 * Save sql query 359 */ 360 function sql_save($query, &$query_result, $ttl) 361 { 362 global $db, $phpEx; 363 364 // Remove extra spaces and tabs 365 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); 366 367 if ($fp = @fopen($this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx, 'wb')) 368 { 369 @flock($fp, LOCK_EX); 370 371 $lines = array(); 372 $query_id = sizeof($this->sql_rowset); 373 $this->sql_rowset[$query_id] = array(); 374 $this->sql_row_pointer[$query_id] = 0; 375 376 while ($row = $db->sql_fetchrow($query_result)) 377 { 378 $this->sql_rowset[$query_id][] = $row; 379 380 $lines[] = "unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($row))) . "')"; 381 } 382 $db->sql_freeresult($query_result); 383 384 fwrite($fp, "<?php\n\n/*\n" . str_replace('*/', '*\/', $query) . "\n*/\n\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$this->sql_rowset[\$query_id] = array(" . implode(',', $lines) . ') ?>'); 385 @flock($fp, LOCK_UN); 386 fclose($fp); 387 388 $query_result = $query_id; 389 } 390 } 391 392 /** 393 * Ceck if a given sql query exist in cache 394 */ 395 function sql_exists($query_id) 396 { 397 return isset($this->sql_rowset[$query_id]); 398 } 399 400 /** 401 * Fetch row from cache (database) 402 */ 403 function sql_fetchrow($query_id) 404 { 405 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) 406 { 407 return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++]; 408 } 409 410 return false; 411 } 412 413 /** 414 * Fetch a field from the current row of a cached database result (database) 415 */ 416 function sql_fetchfield($query_id, $field) 417 { 418 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) 419 { 420 return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false; 421 } 422 423 return false; 424 } 425 426 /** 427 * Seek a specific row in an a cached database result (database) 428 */ 429 function sql_rowseek($rownum, $query_id) 430 { 431 if ($rownum >= sizeof($this->sql_rowset[$query_id])) 432 { 433 return false; 434 } 435 436 $this->sql_row_pointer[$query_id] = $rownum; 437 return true; 438 } 439 440 /** 441 * Free memory used for a cached database result (database) 442 */ 443 function sql_freeresult($query_id) 444 { 445 if (!isset($this->sql_rowset[$query_id])) 446 { 447 return false; 448 } 449 450 unset($this->sql_rowset[$query_id]); 451 unset($this->sql_row_pointer[$query_id]); 452 453 return true; 454 } 455 } 456 457 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 22 00:35:05 2006 | Cross-referenced by PHPXref 0.6 |