forked from Gregwar/Cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.php
executable file
·366 lines (321 loc) · 9.7 KB
/
Cache.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
namespace Gregwar\Cache;
/**
* A cache system based on files
*
* @author Gregwar <[email protected]>
*/
class Cache implements CacheInterface
{
/**
* Cache directory
* @var string
*/
protected $cacheDirectory;
/**
* Use a different directory as actual cache
* @var string|null
*/
protected $actualCacheDirectory;
/**
* Prefix directories size
*
* For instance, if the file is helloworld.txt and the prefix size is
* 5, the cache file will be: h/e/l/l/o/helloworld.txt
*
* This is useful to avoid reaching a too large number of files into the
* cache system directories
* @var int
*/
protected $prefixSize = 5;
/**
* Directory mode
*
* Allows setting of the access mode for the directories created.
* @var int
*/
protected $directoryMode = 0755;
/**
* Constructs the cache system
*
* @param string $cacheDirectory the cache directory
*/
public function __construct($cacheDirectory = 'cache')
{
$this->cacheDirectory = $cacheDirectory;
}
/**
* Sets the cache directory
*
* @param string $cacheDirectory the cache directory
* @return self
*/
public function setCacheDirectory($cacheDirectory)
{
$this->cacheDirectory = $cacheDirectory;
return $this;
}
/**
* Gets the cache directory
*
* @return string the cache directory
*/
public function getCacheDirectory()
{
return $this->cacheDirectory;
}
/**
* Sets the actual cache directory
*
* @param string|null $actualCacheDirectory the actual cache directory
* @return self
*/
public function setActualCacheDirectory($actualCacheDirectory = null)
{
$this->actualCacheDirectory = $actualCacheDirectory;
return $this;
}
/**
* Returns the actual cache directory
*/
public function getActualCacheDirectory()
{
return $this->actualCacheDirectory ?: $this->cacheDirectory;
}
/**
* Change the prefix size
*
* @param int $prefixSize the size of the prefix directories
* @return self
*/
public function setPrefixSize($prefixSize)
{
$this->prefixSize = $prefixSize;
return $this;
}
/**
* Change the directory mode
*
* @param int $directoryMode the directory mode to use
* @return self
*/
public function setDirectoryMode($directoryMode)
{
if (!$directoryMode) {
$directoryMode = 0755;
}
$this->directoryMode = $directoryMode;
return $this;
}
/**
* Creates a directory
*
* @param string $directory the target directory
*/
protected function mkdir($directory)
{
if (!is_dir($directory)) {
@mkdir($directory, $this->directoryMode, true);
}
}
/**
* Gets the cache file name
*
* @param string $filename the name of the cache file
* @param bool $actual get the actual file or the public file
* @param bool $mkdir a boolean to enable/disable the construction of the
* cache file directory
* @return string
*/
public function getCacheFile($filename, $actual = false, $mkdir = false)
{
$path = array();
// Getting the length of the filename before the extension
$parts = explode('.', $filename);
$len = strlen($parts[0]);
for ($i=0; $i<min($len, $this->prefixSize); $i++) {
$path[] = $filename[$i];
}
$path = implode('/', $path);
if ($mkdir) {
$actualDir = $this->getActualCacheDirectory() . '/' . $path;
$this->mkdir($actualDir);
}
$path .= '/' . $filename;
if ($actual) {
return $this->getActualCacheDirectory() . '/' . $path;
} else {
return $this->getCacheDirectory() . '/' . $path;
}
}
/**
* Checks that the cache conditions are respected
*
* @param string $cacheFile the cache file
* @param array $conditions an array of conditions to check
* @return bool
* @throws \Exception
*/
protected function checkConditions($cacheFile, array $conditions = array())
{
// Implicit condition: the cache file should exist
if (!file_exists($cacheFile)) {
return false;
}
foreach ($conditions as $type => $value) {
switch ($type) {
case 'maxage':
case 'max-age':
// Return false if the file is older than $value
$age = time() - filemtime($cacheFile);
if ($age > $value) {
return false;
}
break;
case 'younger-than':
case 'youngerthan':
// Return false if the file is older than the file $value, or the files $value
$check = function($filename) use ($cacheFile) {
return !file_exists($filename) || filemtime($cacheFile) < filemtime($filename);
};
if (!is_array($value)) {
if (!$this->isRemote($value) && $check($value)) {
return false;
}
} else {
foreach ($value as $file) {
if (!$this->isRemote($file) && $check($file)) {
return false;
}
}
}
break;
default:
throw new \Exception('Cache condition '.$type.' not supported');
}
}
return true;
}
/**
* Checks if the target filename exists in the cache and if the conditions
* are respected
*
* @param string $filename the filename
* @param array $conditions the conditions to respect
* @return bool
*/
public function exists($filename, array $conditions = array())
{
$cacheFile = $this->getCacheFile($filename, true);
return $this->checkConditions($cacheFile, $conditions);
}
/**
* Alias for exists
*
* @param string $filename the filename
* @param array $conditions the conditions to respect
* @return bool
*/
public function check($filename, array $conditions = array())
{
return $this->exists($filename, $conditions);
}
/**
* Write data in the cache
*
* @param string $filename the name of the cache file
* @param string $contents the contents to store
* @return self
*/
public function set($filename, $contents = '')
{
$cacheFile = $this->getCacheFile($filename, true, true);
file_put_contents($cacheFile, $contents, \LOCK_EX);
return $this;
}
/**
* Alias for set()
*
* @param string $filename the name of the cache file
* @param string $contents the contents to store
* @return self
*/
public function write($filename, $contents = '')
{
return $this->set($filename, $contents);
}
/**
* Get data from the cache
*
* @param string $filename the cache file name
* @param array $conditions
* @return null|string
*/
public function get($filename, array $conditions = array())
{
if ($this->exists($filename, $conditions)) {
return file_get_contents($this->getCacheFile($filename, true));
} else {
return null;
}
}
/**
* Is this URL remote?
*
* @param string $file
* @return bool
*/
protected function isRemote($file)
{
if (preg_match('/^([a-z]+):\/\//', $file, $match)) {
return ($match[1] != 'file');
}
return false;
}
/**
* Get or create the cache entry
*
* @param string $filename the cache file name
* @param array $conditions an array of conditions about expiration
* @param \Closure $function the closure to call if the file does not exist
* @param bool $file returns the cache file or the file contents
* @param bool $actual returns the actual cache file
* @return string
* @throws \InvalidArgumentException
*/
public function getOrCreate($filename, array $conditions, $function, $file = false, $actual = false)
{
if (!is_callable($function)) {
throw new \InvalidArgumentException('The argument $function should be callable');
}
$cacheFile = $this->getCacheFile($filename, true, true);
$data = null;
if (!$this->check($filename, $conditions)) {
if(file_exists($cacheFile)) {
unlink($cacheFile);
}
$data = call_user_func($function, $cacheFile);
// Test if the closure wrote the file or if it returned the data
if (!file_exists($cacheFile)) {
$this->set($filename, $data);
} else {
$data = file_get_contents($cacheFile);
}
}
return $file ? $this->getCacheFile($filename, $actual) : file_get_contents($cacheFile);
}
/**
* Alias to getOrCreate with $file = true
*
* @param string $filename the cache file name
* @param array $conditions an array of conditions about expiration
* @param \Closure $function the closure to call if the file does not exist
* @param bool $actual returns the actual cache file
* @return string
* @throws \InvalidArgumentException
*/
public function getOrCreateFile($filename, array $conditions, $function, $actual = false)
{
return $this->getOrCreate($filename, $conditions, $function, true, $actual);
}
}