1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
class Cache |
8
|
|
|
{ |
9
|
|
|
private $cache = []; |
10
|
|
|
private $converter; |
11
|
|
|
|
12
|
|
|
public function __construct(Filesystem $fs, Converter $converter) |
13
|
|
|
{ |
14
|
|
|
$this->converter = $converter; |
15
|
|
|
$this->path = $fs->getPath('.cache'); |
|
|
|
|
16
|
|
|
if (!is_dir($this->path)) { |
17
|
|
|
mkdir($this->path); |
18
|
|
|
@chmod($this->path, 0777); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
2 |
|
public function clear() |
24
|
|
|
{ |
25
|
2 |
|
foreach (scandir($this->path) as $f) { |
26
|
2 |
|
if ($f != '.' && $f != '..') { |
27
|
2 |
|
unlink($this->path.'/'.$f); |
28
|
|
|
} |
29
|
|
|
} |
30
|
2 |
|
} |
31
|
|
|
|
32
|
56 |
|
public function exists($key) |
33
|
|
|
{ |
34
|
56 |
|
if (!is_string($key)) { |
35
|
|
|
$key = md5(json_encode($key)); |
36
|
|
|
} |
37
|
|
|
|
38
|
56 |
|
if (array_key_exists($key, $this->cache)) { |
39
|
5 |
|
return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp(); |
|
|
|
|
40
|
|
|
} |
41
|
56 |
|
$filename = $this->path . '/' . $key; |
42
|
56 |
|
if (file_exists($filename) && filesize($filename)) { |
43
|
2 |
|
if (!array_key_exists($key, $this->cache)) { |
44
|
2 |
|
$this->cache[$key] = include $filename; |
45
|
|
|
} |
46
|
2 |
|
$value = $this->cache[$key]; |
47
|
2 |
|
return !array_key_exists('expire', $value) || $value['expire'] > Carbon::now()->getTimestamp(); |
48
|
|
|
} |
49
|
56 |
|
} |
50
|
|
|
|
51
|
5 |
|
public function get($key) |
52
|
|
|
{ |
53
|
5 |
|
if ($this->exists($key)) { |
54
|
5 |
|
return $this->converter->toObject($this->cache[$key]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
public function set($key, $value) |
59
|
|
|
{ |
60
|
5 |
|
$filename = $this->path . '/' . $key; |
61
|
5 |
|
$data = $this->converter->toArray($value); |
62
|
5 |
|
$string = '<?php return '.var_export($data, true).';'; |
63
|
|
|
|
64
|
5 |
|
file_put_contents($filename, $string); |
65
|
5 |
|
@chmod($filename, 0777); |
66
|
|
|
|
67
|
5 |
|
$this->cache[$key] = $data; |
68
|
5 |
|
} |
69
|
|
|
|
70
|
56 |
|
public function wrap($key, $callback) |
71
|
|
|
{ |
72
|
56 |
|
if (!is_string($key)) { |
73
|
56 |
|
$key = md5(json_encode($key)); |
74
|
|
|
} |
75
|
56 |
|
if ($this->exists($key)) { |
76
|
5 |
|
return $this->get($key); |
77
|
|
|
} |
78
|
56 |
|
$result = call_user_func($callback); |
79
|
56 |
|
$expire = null; |
80
|
56 |
|
if (is_array($result) && array_key_exists('expire', $result)) { |
81
|
|
|
$expire = $result['expire']; |
82
|
56 |
|
} elseif (is_object($result) && property_exists($result, 'expire')) { |
83
|
4 |
|
$expire = $result->expire; |
84
|
|
|
} |
85
|
56 |
|
if ($expire && $expire > Carbon::now()->getTimestamp()) { |
|
|
|
|
86
|
4 |
|
$this->set($key, $result); |
87
|
|
|
} |
88
|
56 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: