forked from auth0/laravel-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheBridgeAbstract.php
203 lines (157 loc) · 4.67 KB
/
CacheBridgeAbstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace Auth0\Laravel\Bridges;
use DateTimeInterface;
use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Cache\Store;
use Psr\Cache\CacheItemInterface;
use RuntimeException;
use function is_string;
/**
* @api
*/
abstract class CacheBridgeAbstract extends BridgeAbstract
{
/**
* @var array<array{item: CacheItemInterface, expiration: null|DateTimeInterface|int}>
*/
protected array $deferred = [];
final public function clear(): bool
{
$this->deferred = [];
return $this->getCache()->flush();
}
final public function commit(): bool
{
$success = true;
foreach (array_keys($this->deferred) as $singleDeferred) {
$item = $this->getDeferred((string) $singleDeferred);
// @codeCoverageIgnoreStart
if ($item instanceof CacheItemInterface && ! $this->save($item)) {
$success = false;
}
// @codeCoverageIgnoreEnd
}
$this->deferred = [];
return $success;
}
/**
* @param string $key the key for which to return the corresponding Cache Item
*/
final public function deleteItem(string $key): bool
{
return $this->getCache()->forget($key);
}
final public function deleteItems(array $keys): bool
{
$deleted = true;
foreach ($keys as $key) {
if (! $this->deleteItem($key)) {
$deleted = false;
}
}
return $deleted;
}
final public function getItem(string $key): CacheItemInterface
{
$value = $this->getCache()->get($key);
if (false === $value) {
return CacheItemBridge::miss($key);
}
return $this->createItem($key, $value);
}
/**
* @param string[] $keys
*
* @return CacheItemInterface[]
*/
final public function getItems(array $keys = []): iterable
{
if ([] === $keys) {
return [];
}
$results = $this->getCache()->many($keys);
$items = [];
foreach ($results as $key => $value) {
$key = (string) $key;
$items[$key] = $this->createItem($key, $value);
}
return $items;
}
/**
* @param string $key the key for which to return the corresponding Cache Item
*/
final public function hasItem(string $key): bool
{
return $this->getItem($key)
->isHit();
}
final public function save(CacheItemInterface $item): bool
{
if (! $item instanceof CacheItemBridge) {
return false;
}
$value = serialize($item->getRawValue());
$key = $item->getKey();
$expires = $item->getExpiration();
if ($expires->getTimestamp() <= time()) {
return $this->deleteItem($key);
}
$ttl = $expires->getTimestamp() - time();
return $this->getCache()->put($key, $value, $ttl);
}
final public function saveDeferred(CacheItemInterface $item): bool
{
if (! $item instanceof CacheItemBridge) {
return false;
}
$this->deferred[$item->getKey()] = [
'item' => $item,
'expiration' => $item->getExpiration(),
];
return true;
}
protected function createItem(string $key, mixed $value): CacheItemInterface
{
if (! is_string($value)) {
return CacheItemBridge::miss($key);
}
$value = unserialize($value);
if (false === $value) {
return CacheItemBridge::miss($key);
}
return new CacheItemBridge($key, $value, true);
}
protected function getCache(): Store
{
$cache = cache();
// @codeCoverageIgnoreStart
if (! $cache instanceof CacheManager) {
throw new RuntimeException('Cache store is not an instance of Illuminate\Contracts\Cache\CacheManager');
}
// @codeCoverageIgnoreEnd
return $cache->getStore();
}
/**
* @param string $key the key for which to return the corresponding Cache Item
*
* @codeCoverageIgnore
*/
protected function getDeferred(string $key): ?CacheItemInterface
{
if (! isset($this->deferred[$key])) {
return null;
}
$deferred = $this->deferred[$key];
$item = clone $deferred['item'];
$expires = $deferred['expiration'];
if ($expires instanceof DateTimeInterface) {
$expires = $expires->getTimestamp();
}
if (null !== $expires && $expires <= time()) {
unset($this->deferred[$key]);
return null;
}
return $item;
}
}