-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.class.php
More file actions
207 lines (170 loc) · 5.44 KB
/
Copy pathCache.class.php
File metadata and controls
207 lines (170 loc) · 5.44 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* 缓存管理类
* 支持文件缓存,具备缓存键生成、过期控制等功能
*/
class Cache {
private static $cacheDir;
private static $defaultTTL = 300;
private static function init() {
if (self::$cacheDir === null) {
if (defined('CACHE_DIR')) {
self::$cacheDir = CACHE_DIR;
} else {
self::$cacheDir = __DIR__ . '/cache';
}
if (!is_dir(self::$cacheDir)) {
@mkdir(self::$cacheDir, 0755, true);
}
}
}
public static function setCacheDir($dir) {
self::$cacheDir = rtrim($dir, '/\\');
self::init();
}
public static function setDefaultTTL($seconds) {
self::$defaultTTL = (int)$seconds;
}
public static function getCacheKey($prefix, $params = []) {
$parts = [$prefix];
foreach ($params as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
}
$parts[] = $key . '_' . md5((string)$value);
}
return implode('_', $parts);
}
public static function get($key, $default = null) {
self::init();
$file = self::getCacheFile($key);
if (!file_exists($file)) {
return $default;
}
$content = file_get_contents($file);
if ($content === false) {
return $default;
}
$data = @unserialize($content, ['allowed_classes' => false]);
if ($data === false) {
@unlink($file);
return $default;
}
if (!isset($data['expires']) || !isset($data['value'])) {
@unlink($file);
return $default;
}
if ($data['expires'] > 0 && $data['expires'] < time()) {
@unlink($file);
return $default;
}
return $data['value'];
}
public static function set($key, $value, $ttl = null) {
self::init();
$file = self::getCacheFile($key);
$dir = dirname($file);
if (!is_dir($dir)) {
@mkdir($dir, 0755, true);
}
$ttl = $ttl !== null ? (int)$ttl : self::$defaultTTL;
$expires = $ttl > 0 ? time() + $ttl : 0;
$data = [
'expires' => $expires,
'value' => $value
];
return @file_put_contents($file, serialize($data), LOCK_EX) !== false;
}
public static function delete($key) {
self::init();
$file = self::getCacheFile($key);
return file_exists($file) && @unlink($file);
}
public static function clear($prefix = null) {
self::init();
$result = true;
if ($prefix === null) {
$files = glob(self::$cacheDir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
$result = $result && @unlink($file);
}
}
} else {
$files = glob(self::$cacheDir . '/' . $prefix . '_*.cache');
foreach ($files as $file) {
if (is_file($file)) {
$result = $result && @unlink($file);
}
}
}
return $result;
}
public static function exists($key) {
self::init();
$file = self::getCacheFile($key);
if (!file_exists($file)) {
return false;
}
$content = @file_get_contents($file);
if ($content === false) {
return false;
}
$data = @unserialize($content, ['allowed_classes' => false]);
if ($data === false || !isset($data['expires'])) {
return false;
}
if ($data['expires'] > 0 && $data['expires'] < time()) {
@unlink($file);
return false;
}
return true;
}
public static function remember($key, $ttl, $callback) {
$value = self::get($key);
if ($value !== null) {
return $value;
}
$value = $callback();
self::set($key, $value, $ttl);
return $value;
}
private static function getCacheFile($key) {
$safeKey = preg_replace('/[^a-zA-Z0-9_-]/', '_', $key);
return self::$cacheDir . '/' . $safeKey . '.cache';
}
public static function gc($maxLifetime = 86400) {
self::init();
$files = glob(self::$cacheDir . '/*.cache');
$now = time();
$deleted = 0;
foreach ($files as $file) {
if (is_file($file)) {
$mtime = filemtime($file);
if ($now - $mtime > $maxLifetime) {
@unlink($file);
$deleted++;
}
}
}
return $deleted;
}
}
/**
* 简单的缓存辅助函数
*/
function cache_get($key, $default = null) {
return Cache::get($key, $default);
}
function cache_set($key, $value, $ttl = null) {
return Cache::set($key, $value, $ttl);
}
function cache_delete($key) {
return Cache::delete($key);
}
function cache_clear($prefix = null) {
return Cache::clear($prefix);
}
function cache_remember($key, $ttl, $callback) {
return Cache::remember($key, $ttl, $callback);
}