-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupercache.filedriver.php
298 lines (277 loc) · 6.4 KB
/
supercache.filedriver.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* Super Cache module: File cache driver
*
* Copyright (c) 2016 Kijin Sung <[email protected]>
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class SuperCacheFileDriver
{
/**
* The cache directory.
*/
protected $_dir;
/**
* Constructor.
*/
public function __construct()
{
$this->_dir = _XE_PATH_ . 'files/supercache';
if (!file_exists($this->_dir))
{
FileHandler::makeDir($this->_dir);
}
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @param int $max_age
* @return mixed
*/
public function get($key, $max_age = 0)
{
// Get filename.
$filename = $this->getFilename($key);
if (!file_exists($filename))
{
return false;
}
// Get data from file.
$data = (include $filename);
// Don't accept expired or invalid data.
if (!is_array($data) || count($data) < 2 || ($data[0] > 0 && $data[0] < time()))
{
@unlink($filename);
return false;
}
else
{
return $data[1];
}
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function put($key, $value, $ttl = 0)
{
// Get filename.
$filename = $this->getFilename($key);
$filedir = dirname($filename);
if (!file_exists($filedir))
{
FileHandler::makeDir($filedir);
}
// Encode the data.
$data = array($ttl ? (time() + $ttl) : 0, $value);
$data = '<' . '?php /* ' . $key . ' */' . PHP_EOL . 'return unserialize(' . var_export(serialize($data), true) . ');' . PHP_EOL;
// Write to a temp file and atomically rename it over the target.
$tmpfilename = $filename . '.tmp.' . microtime(true);
$result = @file_put_contents($tmpfilename, $data, LOCK_EX);
if (!$result)
{
return false;
}
$result = @rename($tmpfilename, $filename);
if (!$result)
{
@unlink($filename);
$result = @rename($tmpfilename, $filename);
}
// Invalidate opcache for the cache file.
if (function_exists('opcache_invalidate'))
{
@opcache_invalidate($filename, true);
}
return $result ? true : false;
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return @unlink($this->getFilename($key));
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function isValid($key)
{
return $this->get($key) !== null;
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
$value = intval($this->get($key));
$success = $this->put($key, $value + $amount, 0);
return $success ? ($value + $amount) : false;
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
return $this->incr($key, 0 - $amount);
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function truncate()
{
// Try to rename the cache directory first.
$tempdirname = $this->_dir . '_' . time();
$renamed = @rename($this->_dir, $tempdirname);
if (!$renamed)
{
return false;
}
// Delete the old cache directory.
return $this->deleteDirectory($tempdirname);
}
/**
* Get the filename to store a key.
*
* @param string $key
* @return string
*/
public function getFilename($key)
{
$key = strtr($key, ':', '/');
return $this->_dir . '/' . $key . '.php';
}
/**
* Get a group key.
*
* This method simply returns the key.
*
* @param string $group_key
* @param string $key
* @return string
*/
public function getGroupKey($group_key, $key)
{
return $key;
}
/**
* Invalidate a group key.
*
* This method clears the cache.
*
* @param string $group_key
* @return bool
*/
public function invalidateGroupKey($group_key)
{
return $this->truncate();
}
/**
* Invalidate a subgroup key.
*
* This method deletes the directory associated with the subgroup key.
*
* @param string $subgroup_key
* @param int $index
* @return bool
*/
public function invalidateSubgroupKey($subgroup_key, $index)
{
return $this->deleteDirectory($this->_dir . '/' . strtr($subgroup_key, ':', '/') . '/' . $index, false);
}
/**
* Delete a directory.
*
* This method tries to use system commands to delete a directory quickly,
* but falls back to XE functions if this doesn't work.
*
* @param string $dir
* @param bool $fallback
* @return bool
*/
public function deleteDirectory($dir, $fallback = true)
{
// Try to delete the renamed directory using system commands.
if (function_exists('exec') && !preg_match('/(?<!_)exec/', ini_get('disable_functions')))
{
if (strncasecmp(\PHP_OS, 'win', 3) == 0)
{
@exec('rmdir /S /Q ' . escapeshellarg($dir));
}
else
{
@exec('rm -rf ' . escapeshellarg($dir));
}
}
// Try to delete the renamed directory using XE functions.
if (file_exists($dir))
{
if ($fallback)
{
FileHandler::removeDir($dir);
clearstatcache($dir);
return file_exists($dir);
}
else
{
return false;
}
}
else
{
return true;
}
}
}