1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Cache; |
6
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Psr\SimpleCache\CacheInterface; |
9
|
|
|
use Spiral\Cache\Event\CacheHit; |
10
|
|
|
use Spiral\Cache\Event\CacheMissed; |
11
|
|
|
use Spiral\Cache\Event\CacheRetrieving; |
12
|
|
|
use Spiral\Cache\Event\KeyDeleted; |
13
|
|
|
use Spiral\Cache\Event\KeyDeleteFailed; |
14
|
|
|
use Spiral\Cache\Event\KeyDeleting; |
15
|
|
|
use Spiral\Cache\Event\KeyWriteFailed; |
16
|
|
|
use Spiral\Cache\Event\KeyWriting; |
17
|
|
|
use Spiral\Cache\Event\KeyWritten; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @internal |
21
|
|
|
*/ |
22
|
|
|
class CacheRepository implements CacheInterface |
23
|
|
|
{ |
24
|
56 |
|
public function __construct( |
25
|
|
|
protected CacheInterface $storage, |
26
|
|
|
protected ?EventDispatcherInterface $dispatcher = null, |
27
|
|
|
protected ?string $prefix = null |
28
|
|
|
) { |
29
|
56 |
|
} |
30
|
|
|
|
31
|
13 |
|
public function get(string $key, mixed $default = null): mixed |
32
|
|
|
{ |
33
|
13 |
|
$key = $this->resolveKey($key); |
34
|
|
|
|
35
|
13 |
|
$this->dispatcher?->dispatch(new CacheRetrieving($key)); |
36
|
|
|
|
37
|
13 |
|
$value = $this->storage->get($key); |
38
|
|
|
|
39
|
13 |
|
if ($value === null) { |
40
|
10 |
|
$this->dispatcher?->dispatch(new CacheMissed($key)); |
41
|
|
|
|
42
|
10 |
|
return $default; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
$this->dispatcher?->dispatch(new CacheHit($key, $value)); |
46
|
|
|
|
47
|
3 |
|
return $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
15 |
|
public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool |
51
|
|
|
{ |
52
|
15 |
|
$key = $this->resolveKey($key); |
53
|
|
|
|
54
|
15 |
|
$this->dispatcher?->dispatch(new KeyWriting($key, $value)); |
55
|
|
|
|
56
|
15 |
|
$result = $this->storage->set($key, $value, $ttl); |
57
|
|
|
|
58
|
15 |
|
$this->dispatcher?->dispatch(match ($result) { |
59
|
4 |
|
true => new KeyWritten($key, $value), |
60
|
6 |
|
default => new KeyWriteFailed($key, $value), |
61
|
15 |
|
}); |
62
|
|
|
|
63
|
15 |
|
return $result; |
64
|
|
|
} |
65
|
|
|
|
66
|
12 |
|
public function delete(string $key): bool |
67
|
|
|
{ |
68
|
12 |
|
$key = $this->resolveKey($key); |
69
|
|
|
|
70
|
12 |
|
$this->dispatcher?->dispatch(new KeyDeleting($key)); |
71
|
|
|
|
72
|
12 |
|
$result = $this->storage->delete($key); |
73
|
|
|
|
74
|
12 |
|
$this->dispatcher?->dispatch(match ($result) { |
75
|
2 |
|
true => new KeyDeleted($key), |
76
|
4 |
|
default => new KeyDeleteFailed($key), |
77
|
12 |
|
}); |
78
|
|
|
|
79
|
12 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function clear(): bool |
83
|
|
|
{ |
84
|
|
|
return $this->storage->clear(); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
88
|
|
|
{ |
89
|
6 |
|
$result = []; |
90
|
|
|
|
91
|
6 |
|
foreach ($keys as $key) { |
92
|
6 |
|
$result[$key] = $this->get($key, $default); |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool |
99
|
|
|
{ |
100
|
7 |
|
$state = null; |
101
|
|
|
|
102
|
7 |
|
foreach ($values as $key => $value) { |
103
|
7 |
|
$result = $this->set($key, $value, $ttl); |
104
|
7 |
|
$state = \is_null($state) ? $result : $result && $state; |
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
return $state ?: false; |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
public function deleteMultiple(iterable $keys): bool |
111
|
|
|
{ |
112
|
6 |
|
$state = null; |
113
|
6 |
|
foreach ($keys as $key) { |
114
|
6 |
|
$result = $this->delete($key); |
115
|
6 |
|
$state = \is_null($state) ? $result : $result && $state; |
116
|
|
|
} |
117
|
|
|
|
118
|
6 |
|
return $state ?: false; |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
public function has(string $key): bool |
122
|
|
|
{ |
123
|
5 |
|
return $this->storage->has($this->resolveKey($key)); |
124
|
|
|
} |
125
|
|
|
|
126
|
15 |
|
public function getStorage(): CacheInterface |
127
|
|
|
{ |
128
|
15 |
|
return $this->storage; |
129
|
|
|
} |
130
|
|
|
|
131
|
39 |
|
private function resolveKey(string $key): string |
132
|
|
|
{ |
133
|
39 |
|
if (!empty($this->prefix)) { |
134
|
7 |
|
return $this->prefix . $key; |
135
|
|
|
} |
136
|
|
|
|
137
|
32 |
|
return $key; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|