cache_file.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: cache_file.php 6757 2010-03-25 09:01:29Z cnteacher $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class ultrax_cache {
  12. function ultrax_cache($conf) {
  13. $this->conf = $conf;
  14. }
  15. function get_cache($key) {
  16. if($this->cache_exists($key)) {
  17. $data = $this->_get_cache($key);
  18. return $data['data'];
  19. }
  20. return false;
  21. }
  22. function set_cache($key, $value, $life) {
  23. global $_G;
  24. $data = array($key => array('data' => $value, 'life' => $life));
  25. require_once libfile('function/cache');
  26. $cache_file = $this->get_cache_file_path($key);
  27. dmkdir(dirname($cache_file));
  28. $cachedata = "\$data = ".arrayeval($data).";\n";
  29. if($fp = @fopen($cache_file, 'wb')) {
  30. fwrite($fp, "<?php\n//Discuz! cache file, DO NOT modify me!".
  31. "\n//Created: ".date("M j, Y, G:i").
  32. "\n//Identify: ".md5($cache_file.$cachedata.$_G['config']['security']['authkey'])."\n\nif(!defined('IN_DISCUZ')) {\n\texit('Access Denied');\n}\n\n$cachedata?>");
  33. fclose($fp);
  34. } else {
  35. exit('Can not write to cache files, please check directory ./data/ and ./data/ultraxcache/ .');
  36. }
  37. return true;
  38. }
  39. function del_cache($key) {
  40. $cache_file = $this->get_cache_file_path($key);
  41. if(file_exists($cache_file)) {
  42. return @unlink($cache_file);
  43. }
  44. return true;
  45. }
  46. function _get_cache($key) {
  47. static $data = null;
  48. if(!isset($data[$key])) {
  49. include $this->get_cache_file_path($key);
  50. }
  51. return $data[$key];
  52. }
  53. function cache_exists($key) {
  54. $cache_file = $this->get_cache_file_path($key);
  55. if(!file_exists($cache_file)) {
  56. return false;
  57. }
  58. $data = $this->_get_cache($key);
  59. if($data['life'] && (filemtime($cache_file) < time() - $data['life'])) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. function get_cache_file_path($key) {
  65. static $cache_path = null;
  66. if(!isset($cache_path[$key])) {
  67. $dir = hexdec($key{0}.$key{1}.$key{2}) % 1000;
  68. $cache_path[$key] = $this->conf['path'].'/'.$dir.'/'.$key.'.php';
  69. }
  70. return $cache_path[$key];
  71. }
  72. }