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