|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace voku\cache; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* AdapterArray: simple array-cache adapter |
|
9
|
|
|
*/ |
|
10
|
|
|
class AdapterArray implements iAdapter |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $values = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $expired = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
13 |
|
public function exists(string $key): bool |
|
26
|
|
|
{ |
|
27
|
13 |
|
$this->removeExpired($key); |
|
28
|
|
|
|
|
29
|
13 |
|
return \array_key_exists($key, self::$values); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
11 |
|
public function get(string $key) |
|
36
|
|
|
{ |
|
37
|
11 |
|
return $this->exists($key) ? self::$values[$key] : null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function installed(): bool |
|
44
|
|
|
{ |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function remove(string $key): bool |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->removeExpired($key); |
|
54
|
|
|
|
|
55
|
1 |
|
if (\array_key_exists($key, self::$values) === true) { |
|
56
|
1 |
|
unset(self::$values[$key]); |
|
57
|
|
|
|
|
58
|
1 |
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function removeAll(): bool |
|
68
|
|
|
{ |
|
69
|
1 |
|
self::$values = []; |
|
70
|
1 |
|
self::$expired = []; |
|
71
|
|
|
|
|
72
|
1 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public function set(string $key, $value): bool |
|
79
|
|
|
{ |
|
80
|
6 |
|
self::$values[$key] = $value; |
|
81
|
|
|
|
|
82
|
6 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function setExpired(string $key, $value, int $ttl = 0): bool |
|
89
|
|
|
{ |
|
90
|
4 |
|
self::$values[$key] = $value; |
|
91
|
|
|
|
|
92
|
4 |
|
if ($ttl !== null) { |
|
93
|
4 |
|
self::$expired[$key] = [\time(), $ttl]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Remove expired cache. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $key |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
13 |
|
private function removeExpired($key): bool |
|
107
|
|
|
{ |
|
108
|
|
|
if ( |
|
109
|
13 |
|
\array_key_exists($key, self::$expired) === false |
|
110
|
|
|
|| |
|
111
|
13 |
|
\array_key_exists($key, self::$values) === false |
|
112
|
|
|
) { |
|
113
|
9 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
4 |
|
list($time, $ttl) = self::$expired[$key]; |
|
117
|
|
|
|
|
118
|
4 |
|
if (\time() > ($time + $ttl)) { |
|
119
|
3 |
|
unset(self::$values[$key]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
4 |
|
if (!isset(self::$values[$key])) { |
|
123
|
3 |
|
unset(self::$expired[$key]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|