|
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
|
57 |
|
public function __construct( |
|
25
|
|
|
protected CacheInterface $storage, |
|
26
|
|
|
protected ?EventDispatcherInterface $dispatcher = null, |
|
27
|
|
|
protected ?string $prefix = null |
|
28
|
|
|
) { |
|
29
|
57 |
|
} |
|
30
|
|
|
|
|
31
|
14 |
|
public function get(string $key, mixed $default = null): mixed |
|
32
|
|
|
{ |
|
33
|
14 |
|
$key = $this->resolveKey($key); |
|
34
|
|
|
|
|
35
|
14 |
|
$this->dispatcher?->dispatch(new CacheRetrieving($key)); |
|
36
|
|
|
|
|
37
|
14 |
|
$value = $this->storage->get($key); |
|
38
|
|
|
|
|
39
|
14 |
|
if ($value === null) { |
|
40
|
10 |
|
$this->dispatcher?->dispatch(new CacheMissed($key)); |
|
41
|
|
|
|
|
42
|
10 |
|
return $default; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
$this->dispatcher?->dispatch(new CacheHit($key, $value)); |
|
46
|
|
|
|
|
47
|
4 |
|
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( |
|
59
|
15 |
|
$result |
|
60
|
4 |
|
? new KeyWritten($key, $value) |
|
61
|
6 |
|
: new KeyWriteFailed($key, $value), |
|
62
|
15 |
|
); |
|
63
|
|
|
|
|
64
|
15 |
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
12 |
|
public function delete(string $key): bool |
|
68
|
|
|
{ |
|
69
|
12 |
|
$key = $this->resolveKey($key); |
|
70
|
|
|
|
|
71
|
12 |
|
$this->dispatcher?->dispatch(new KeyDeleting($key)); |
|
72
|
|
|
|
|
73
|
12 |
|
$result = $this->storage->delete($key); |
|
74
|
|
|
|
|
75
|
12 |
|
$this->dispatcher?->dispatch( |
|
76
|
12 |
|
$result |
|
77
|
2 |
|
? new KeyDeleted($key) |
|
78
|
4 |
|
: new KeyDeleteFailed($key), |
|
79
|
12 |
|
); |
|
80
|
|
|
|
|
81
|
12 |
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function clear(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->storage->clear(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
public function getMultiple(iterable $keys, mixed $default = null): iterable |
|
90
|
|
|
{ |
|
91
|
6 |
|
$result = []; |
|
92
|
|
|
|
|
93
|
6 |
|
foreach ($keys as $key) { |
|
94
|
6 |
|
$result[$key] = $this->get($key, $default); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
6 |
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
7 |
|
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool |
|
101
|
|
|
{ |
|
102
|
7 |
|
$state = null; |
|
103
|
|
|
|
|
104
|
7 |
|
foreach ($values as $key => $value) { |
|
105
|
7 |
|
$result = $this->set($key, $value, $ttl); |
|
106
|
7 |
|
$state = \is_null($state) ? $result : $result && $state; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
7 |
|
return $state ?: false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
6 |
|
public function deleteMultiple(iterable $keys): bool |
|
113
|
|
|
{ |
|
114
|
6 |
|
$state = null; |
|
115
|
6 |
|
foreach ($keys as $key) { |
|
116
|
6 |
|
$result = $this->delete($key); |
|
117
|
6 |
|
$state = \is_null($state) ? $result : $result && $state; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
6 |
|
return $state ?: false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
5 |
|
public function has(string $key): bool |
|
124
|
|
|
{ |
|
125
|
5 |
|
return $this->storage->has($this->resolveKey($key)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
15 |
|
public function getStorage(): CacheInterface |
|
129
|
|
|
{ |
|
130
|
15 |
|
return $this->storage; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
40 |
|
private function resolveKey(string $key): string |
|
134
|
|
|
{ |
|
135
|
40 |
|
return ((string) $this->prefix) . $key; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|