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
|
45 |
|
public function exists($key) |
33
|
|
|
{ |
34
|
45 |
|
if (array_key_exists($key, $this->cache)) { |
35
|
5 |
|
return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp(); |
|
|
|
|
36
|
|
|
} |
37
|
45 |
|
$filename = $this->path . '/' . $key; |
38
|
45 |
|
if (file_exists($filename) && filesize($filename)) { |
39
|
2 |
|
if (!array_key_exists($key, $this->cache)) { |
40
|
2 |
|
$this->cache[$key] = include $filename; |
41
|
|
|
} |
42
|
2 |
|
$value = $this->cache[$key]; |
43
|
2 |
|
return !array_key_exists('expire', $value) || $value['expire'] > Carbon::now()->getTimestamp(); |
44
|
|
|
} |
45
|
45 |
|
} |
46
|
|
|
|
47
|
5 |
|
public function get($key) |
48
|
|
|
{ |
49
|
5 |
|
if ($this->exists($key)) { |
50
|
5 |
|
return $this->converter->toObject($this->cache[$key]); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
public function set($key, $value) |
55
|
|
|
{ |
56
|
8 |
|
$filename = $this->path . '/' . $key; |
57
|
8 |
|
$data = $this->converter->toArray($value); |
58
|
8 |
|
$string = '<?php return '.var_export($data, true).';'; |
59
|
|
|
|
60
|
8 |
|
file_put_contents($filename, $string); |
61
|
8 |
|
@chmod($filename, 0777); |
|
|
|
|
62
|
|
|
|
63
|
8 |
|
$this->cache[$key] = $data; |
64
|
8 |
|
} |
65
|
|
|
|
66
|
45 |
|
public function wrap($key, $callback) |
67
|
|
|
{ |
68
|
45 |
|
if (!is_string($key)) { |
69
|
45 |
|
$key = md5(json_encode($key)); |
70
|
|
|
} |
71
|
45 |
|
if ($this->exists($key)) { |
72
|
5 |
|
return $this->get($key); |
73
|
|
|
} |
74
|
45 |
|
$result = call_user_func($callback); |
75
|
45 |
|
$expire = null; |
76
|
45 |
|
if (is_array($result) && array_key_exists('expire', $result)) { |
77
|
|
|
$expire = $result['expire']; |
78
|
45 |
|
} elseif (is_object($result) && property_exists($result, 'expire')) { |
79
|
4 |
|
$expire = $result->expire; |
80
|
|
|
} |
81
|
45 |
|
if ($expire && $expire > Carbon::now()->getTimestamp()) { |
|
|
|
|
82
|
4 |
|
$this->set($key, $result); |
83
|
|
|
} |
84
|
45 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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: