|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache\Psr6; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use Doctrine\Common\Cache\ClearableCache; |
|
7
|
|
|
use Doctrine\Common\Cache\MultiOperationCache; |
|
8
|
|
|
use Psr\Cache\CacheItemInterface; |
|
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class CacheAdapter implements CacheItemPoolInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Cache|ClearableCache|MultiOperationCache */ |
|
14
|
|
|
private $cache; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
|
|
|
|
|
17
|
|
|
* @var CacheItemInterface[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $deferredItems = []; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Cache $cache) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->cache = $cache; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritDoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getItem($key): CacheItemInterface |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$this->assertValidKey($key); |
|
32
|
|
|
|
|
33
|
|
|
if (isset($this->deferredItems[$key])) { |
|
34
|
|
|
return new CacheItem($key, $this->deferredItems[$key]->get()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return new CacheItem($key, $this->cache->fetch($key)); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getItems(array $keys = []): iterable |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$this->assertValidKeys($keys); |
|
46
|
|
|
|
|
47
|
|
|
$fetchedValues = $this->cache->fetchMultiple($keys); |
|
|
|
|
|
|
48
|
|
|
$items = []; |
|
|
|
|
|
|
49
|
|
|
foreach ($keys as $key) { |
|
50
|
|
|
$items[$key] = new CacheItem($key, isset($this->deferredItems[$key]) ? $this->deferredItems[$key]->get() : (\array_key_exists($key, $fetchedValues) ? $fetchedValues[$key] : false)); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $items; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public function hasItem($key): bool |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$this->assertValidKey($key); |
|
62
|
|
|
|
|
63
|
|
|
return isset($this->deferredItems[$key]) |
|
64
|
|
|
? !$this->isExpired($this->deferredItems[$key]) |
|
|
|
|
|
|
65
|
|
|
: $this->cache->contains($key) |
|
|
|
|
|
|
66
|
|
|
; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritDoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function clear(): bool |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$this->deferredItems = []; |
|
75
|
|
|
|
|
76
|
|
|
return $this->cache->deleteAll(); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritDoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function deleteItem($key): bool |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertValidKey($key); |
|
85
|
|
|
unset($this->deferredItems[$key]); |
|
86
|
|
|
|
|
87
|
|
|
return $this->cache->delete($key); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritDoc |
|
92
|
|
|
*/ |
|
93
|
|
|
public function deleteItems(array $keys): bool |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$this->assertValidKeys($keys); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($keys as $key) { |
|
98
|
|
|
unset($this->deferredItems[$key]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->cache->deleteMultiple($keys); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritDoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function save(CacheItemInterface $item): bool |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
unset($this->deferredItems[$item->getKey()]); |
|
110
|
|
|
|
|
111
|
|
|
if ($this->isExpired($item)) { |
|
112
|
|
|
return $this->cache->delete($item->getKey()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->cache->save($item->getKey(), $item->get(), !$item instanceof CacheItem || null === $item->getExpiration() ? 0 : $item->getExpiration()->getTimestamp() - time()); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritDoc |
|
120
|
|
|
*/ |
|
121
|
|
|
public function saveDeferred(CacheItemInterface $item): bool |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->isExpired($item)) { |
|
124
|
|
|
return $this->cache->delete($item->getKey()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->deferredItems[$item->getKey()] = $item; |
|
128
|
|
|
|
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @inheritDoc |
|
134
|
|
|
*/ |
|
135
|
|
|
public function commit(): bool |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
$success = $this->cache->saveMultiple(array_map( |
|
|
|
|
|
|
138
|
|
|
static function (CacheItemInterface $cacheItem) { |
|
139
|
|
|
return $cacheItem->get(); |
|
140
|
|
|
}, |
|
141
|
|
|
$this->deferredItems |
|
142
|
|
|
)); |
|
143
|
|
|
|
|
144
|
|
|
if ($success) { |
|
145
|
|
|
$this->deferredItems = []; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $success; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function __destruct() |
|
152
|
|
|
{ |
|
153
|
|
|
$this->commit(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
private function assertValidKey($key): void |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
if (!\is_string($key) || '' === $key || preg_match('#[\{\}\(\)/\\\\@:]#', $key)) { |
|
|
|
|
|
|
159
|
|
|
throw new InvalidArgumentException('Invalid cache key.'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function assertValidKeys(iterable $keys): void |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
foreach ($keys as $key) { |
|
166
|
|
|
$this->assertValidKey($key); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
private function isExpired(CacheItemInterface $item): bool |
|
|
|
|
|
|
171
|
|
|
{ |
|
172
|
|
|
return $item instanceof CacheItem |
|
173
|
|
|
&& null !== $item->getExpiration() |
|
|
|
|
|
|
174
|
|
|
&& $item->getExpiration() < new \DateTimeImmutable() |
|
|
|
|
|
|
175
|
|
|
; |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|