-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleCache.php
47 lines (41 loc) · 1.7 KB
/
SimpleCache.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
<?php
/* @description A very simple filesystem cache class that stores one entry per file *
* @author Tom Butler tom@r.je *
* @copyright 2015 Tom Butler *
* @license http://www.opensource.org/licenses/bsd-license.php BSD License *
* @version 1.0 */
namespace SimpleCache;
class SimpleCache implements \ArrayAccess {
private $dir;
public function __construct($dir, $maxAge = null, $gcInterval = 86400) {
$this->dir = rtrim(realpath($dir), \DIRECTORY_SEPARATOR);
$this->gc($maxAge, $gcInterval);
}
public function offsetSet($key, $value) {
file_put_contents($this->dir . \DIRECTORY_SEPARATOR . $key, serialize($value));
}
public function offsetGet($key) {
touch($this->dir . \DIRECTORY_SEPARATOR . $key);
if ($this->offsetExists($key)) return unserialize(file_get_contents($this->dir . \DIRECTORY_SEPARATOR . $key));
else return null;
}
public function offsetExists($key) {
return is_file($this->dir . \DIRECTORY_SEPARATOR . $key);
}
public function offsetUnset($key) {
unlink($this->dir . \DIRECTORY_SEPARATOR . $key);
}
private function gc($maxAge, $gcInterval) {
if ($maxAge && rand(1,100) == 1) {
$lastRun = $this['___cacheGC'];
if ($lastRun+$gcInterval < time()) {
foreach (new \DirectoryIterator($this->dir) as $file) {
if (filemtime($this->dir . DIRECTORY_SEPARATOR . $file->getFileName()) + $maxAge < time()) {
unlink($this->dir . DIRECTORY_SEPARATOR . $file->getFileName());
}
}
$this['___cacheGC'] = time();
}
}
}
}