1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Test\Support\SimpleCache; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTime; |
9
|
|
|
use Psr\SimpleCache\CacheInterface; |
10
|
|
|
use Traversable; |
11
|
|
|
use Yiisoft\Test\Support\SimpleCache\Exception\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
final class MemorySimpleCache implements CacheInterface |
14
|
|
|
{ |
15
|
|
|
protected const EXPIRATION_INFINITY = 0; |
16
|
|
|
protected const EXPIRATION_EXPIRED = -1; |
17
|
|
|
|
18
|
|
|
/** @var array<string, array<int, mixed>> */ |
19
|
|
|
protected array $cache = []; |
20
|
|
|
public bool $returnOnDelete = true; |
21
|
|
|
|
22
|
285 |
|
public function __construct(array $cacheData = []) |
23
|
|
|
{ |
24
|
285 |
|
$this->setMultiple($cacheData); |
25
|
285 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
159 |
|
public function get($key, $default = null) |
31
|
|
|
{ |
32
|
159 |
|
$this->validateKey($key); |
33
|
|
|
/** @psalm-var string $key */ |
34
|
143 |
|
if (array_key_exists($key, $this->cache) && !$this->isExpired($key)) { |
35
|
|
|
/** @psalm-var mixed $value */ |
36
|
117 |
|
$value = $this->cache[$key][0]; |
37
|
117 |
|
if (is_object($value)) { |
38
|
16 |
|
$value = clone $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
117 |
|
return $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
53 |
|
return $default; |
45
|
|
|
} |
46
|
|
|
|
47
|
200 |
|
public function set($key, $value, $ttl = null): bool |
48
|
|
|
{ |
49
|
200 |
|
$this->validateKey($key); |
50
|
|
|
/** @psalm-var string $key */ |
51
|
184 |
|
$expiration = $this->ttlToExpiration($ttl); |
52
|
184 |
|
if ($expiration < 0) { |
53
|
2 |
|
return $this->delete($key); |
54
|
|
|
} |
55
|
184 |
|
if (is_object($value)) { |
56
|
42 |
|
$value = clone $value; |
57
|
|
|
} |
58
|
184 |
|
$this->cache[$key] = [$value, $expiration]; |
59
|
184 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
46 |
|
public function delete($key): bool |
63
|
|
|
{ |
64
|
46 |
|
$this->validateKey($key); |
65
|
|
|
/** @psalm-var string $key */ |
66
|
30 |
|
unset($this->cache[$key]); |
67
|
30 |
|
return $this->returnOnDelete; |
68
|
|
|
} |
69
|
|
|
|
70
|
167 |
|
public function clear(): bool |
71
|
|
|
{ |
72
|
167 |
|
$this->cache = []; |
73
|
167 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return mixed[] |
78
|
|
|
*/ |
79
|
30 |
|
public function getMultiple($keys, $default = null): iterable |
80
|
|
|
{ |
81
|
30 |
|
$keys = $this->iterableToArray($keys); |
82
|
29 |
|
$this->validateKeys($keys); |
83
|
|
|
/** @psalm-var string[] $keys */ |
84
|
15 |
|
$result = []; |
85
|
15 |
|
foreach ($keys as $key) { |
86
|
|
|
/** @psalm-var mixed */ |
87
|
15 |
|
$result[$key] = $this->get($key, $default); |
88
|
|
|
} |
89
|
15 |
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
285 |
|
public function setMultiple($values, $ttl = null): bool |
93
|
|
|
{ |
94
|
285 |
|
$values = $this->iterableToArray($values); |
95
|
285 |
|
$this->validateKeysOfValues($values); |
96
|
|
|
/** |
97
|
|
|
* @psalm-var mixed $value |
98
|
|
|
*/ |
99
|
285 |
|
foreach ($values as $key => $value) { |
100
|
26 |
|
$this->set((string) $key, $value, $ttl); |
101
|
|
|
} |
102
|
285 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
18 |
|
public function deleteMultiple($keys): bool |
106
|
|
|
{ |
107
|
18 |
|
$keys = $this->iterableToArray($keys); |
108
|
17 |
|
$this->validateKeys($keys); |
109
|
|
|
/** @var string[] $keys */ |
110
|
3 |
|
foreach ($keys as $key) { |
111
|
3 |
|
$this->delete($key); |
112
|
|
|
} |
113
|
3 |
|
return $this->returnOnDelete; |
114
|
|
|
} |
115
|
|
|
|
116
|
70 |
|
public function has($key): bool |
117
|
|
|
{ |
118
|
70 |
|
$this->validateKey($key); |
119
|
|
|
/** @psalm-var string $key */ |
120
|
54 |
|
return isset($this->cache[$key]) && !$this->isExpired($key); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get stored data |
125
|
|
|
* |
126
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
127
|
|
|
*/ |
128
|
1 |
|
public function getValues(): array |
129
|
|
|
{ |
130
|
1 |
|
$result = []; |
131
|
1 |
|
foreach ($this->cache as $key => $value) { |
132
|
|
|
/** @psalm-var mixed */ |
133
|
1 |
|
$result[$key] = $value[0]; |
134
|
|
|
} |
135
|
1 |
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks whether item is expired or not |
140
|
|
|
*/ |
141
|
133 |
|
private function isExpired(string $key): bool |
142
|
|
|
{ |
143
|
133 |
|
return $this->cache[$key][1] !== 0 && $this->cache[$key][1] <= time(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Converts TTL to expiration |
148
|
|
|
* |
149
|
|
|
* @param DateInterval|int|null $ttl |
150
|
|
|
* |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
184 |
|
private function ttlToExpiration($ttl): int |
154
|
|
|
{ |
155
|
184 |
|
$ttl = $this->normalizeTtl($ttl); |
156
|
|
|
|
157
|
184 |
|
if ($ttl === null) { |
158
|
182 |
|
$expiration = self::EXPIRATION_INFINITY; |
159
|
6 |
|
} elseif ($ttl <= 0) { |
160
|
2 |
|
$expiration = self::EXPIRATION_EXPIRED; |
161
|
|
|
} else { |
162
|
4 |
|
$expiration = $ttl + time(); |
163
|
|
|
} |
164
|
|
|
|
165
|
184 |
|
return $expiration; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Normalizes cache TTL handling strings and {@see DateInterval} objects. |
170
|
|
|
* |
171
|
|
|
* @param DateInterval|int|string|null $ttl raw TTL. |
172
|
|
|
* |
173
|
|
|
* @return int|null TTL value as UNIX timestamp or null meaning infinity |
174
|
|
|
*/ |
175
|
184 |
|
private function normalizeTtl($ttl): ?int |
176
|
|
|
{ |
177
|
184 |
|
if ($ttl instanceof DateInterval) { |
178
|
2 |
|
return (new DateTime('@0'))->add($ttl)->getTimestamp(); |
179
|
|
|
} |
180
|
|
|
|
181
|
184 |
|
if (is_string($ttl)) { |
182
|
|
|
return (int)$ttl; |
183
|
|
|
} |
184
|
|
|
|
185
|
184 |
|
return $ttl; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param mixed $iterable |
190
|
|
|
* |
191
|
|
|
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException |
192
|
|
|
*/ |
193
|
285 |
|
private function iterableToArray($iterable): array |
194
|
|
|
{ |
195
|
285 |
|
if (!is_iterable($iterable)) { |
196
|
3 |
|
throw new InvalidArgumentException(sprintf('Iterable is expected, got %s.', gettype($iterable))); |
197
|
|
|
} |
198
|
285 |
|
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param mixed $key |
203
|
|
|
*/ |
204
|
278 |
|
private function validateKey($key): void |
205
|
|
|
{ |
206
|
278 |
|
if (!\is_string($key) || $key === '' || strpbrk($key, '{}()/\\@:')) { |
207
|
106 |
|
throw new InvalidArgumentException('Invalid key value.'); |
208
|
|
|
} |
209
|
200 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param mixed[] $keys |
213
|
|
|
*/ |
214
|
285 |
|
private function validateKeys(array $keys): void |
215
|
|
|
{ |
216
|
|
|
/** @psalm-var mixed $key */ |
217
|
285 |
|
foreach ($keys as $key) { |
218
|
68 |
|
$this->validateKey($key); |
219
|
|
|
} |
220
|
285 |
|
} |
221
|
|
|
|
222
|
285 |
|
private function validateKeysOfValues(array $values): void |
223
|
|
|
{ |
224
|
285 |
|
$keys = array_map('strval', array_keys($values)); |
225
|
285 |
|
$this->validateKeys($keys); |
226
|
285 |
|
} |
227
|
|
|
} |
228
|
|
|
|