|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Cache\Storage; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ArrayStorage implements CacheInterface |
|
10
|
|
|
{ |
|
11
|
|
|
use InteractsWithTime; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The array of stored values. |
|
15
|
|
|
*/ |
|
16
|
|
|
protected array $storage = []; |
|
17
|
|
|
|
|
18
|
319 |
|
public function __construct( |
|
19
|
|
|
private readonly int $ttl = 2_592_000 |
|
|
|
|
|
|
20
|
|
|
) { |
|
21
|
319 |
|
} |
|
22
|
|
|
|
|
23
|
306 |
|
public function get(string $key, mixed $default = null): mixed |
|
24
|
|
|
{ |
|
25
|
306 |
|
if (!isset($this->storage[$key])) { |
|
26
|
8 |
|
return $default; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
300 |
|
$item = $this->storage[$key]; |
|
30
|
|
|
|
|
31
|
300 |
|
$expiresAt = $item['timestamp'] ?? 0; |
|
32
|
|
|
|
|
33
|
300 |
|
if ($expiresAt !== 0 && \time() >= $expiresAt) { |
|
34
|
3 |
|
$this->delete($key); |
|
35
|
|
|
|
|
36
|
3 |
|
return $default; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
299 |
|
return $item['value']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
311 |
|
public function set(string $key, mixed $value, null|int|\DateInterval|\DateTimeInterface $ttl = null): bool |
|
43
|
|
|
{ |
|
44
|
311 |
|
$this->storage[$key] = [ |
|
45
|
311 |
|
'value' => $value, |
|
46
|
311 |
|
'timestamp' => $this->ttlToTimestamp($ttl), |
|
47
|
311 |
|
]; |
|
48
|
|
|
|
|
49
|
311 |
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
public function delete(string $key): bool |
|
53
|
|
|
{ |
|
54
|
9 |
|
if ($this->has($key)) { |
|
55
|
8 |
|
unset($this->storage[$key]); |
|
56
|
|
|
|
|
57
|
8 |
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function clear(): bool |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->storage = []; |
|
66
|
|
|
|
|
67
|
1 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
|
71
|
|
|
{ |
|
72
|
2 |
|
$return = []; |
|
73
|
|
|
|
|
74
|
2 |
|
foreach ($keys as $key) { |
|
75
|
2 |
|
$return[$key] = $this->get($key, $default); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return $return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function setMultiple(iterable $values, null|int|\DateInterval|\DateTimeInterface $ttl = null): bool |
|
82
|
|
|
{ |
|
83
|
2 |
|
foreach ($values as $key => $value) { |
|
84
|
2 |
|
$this->set($key, $value, $ttl); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function deleteMultiple(iterable $keys): bool |
|
91
|
|
|
{ |
|
92
|
2 |
|
$state = null; |
|
93
|
|
|
|
|
94
|
2 |
|
foreach ($keys as $key) { |
|
95
|
2 |
|
$result = $this->delete($key); |
|
96
|
|
|
|
|
97
|
2 |
|
$state = \is_null($state) ? $result : $result && $state; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
return $state ?: false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
301 |
|
public function has(string $key): bool |
|
104
|
|
|
{ |
|
105
|
301 |
|
return \array_key_exists($key, $this->storage); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|