1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
4
|
|
|
|
5
|
|
|
use function array_combine; |
6
|
|
|
use function array_key_exists; |
7
|
|
|
use function array_map; |
8
|
|
|
use function sprintf; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base class for cache provider implementations. |
12
|
|
|
*/ |
13
|
|
|
abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiOperationCache |
14
|
|
|
{ |
15
|
|
|
public const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The namespace to prefix all cache ids with. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $namespace = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The namespace version. |
26
|
|
|
* |
27
|
|
|
* @var int|null |
28
|
|
|
*/ |
29
|
|
|
private $namespaceVersion; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sets the namespace to prefix all cache ids with. |
33
|
|
|
* |
34
|
|
|
* @param string $namespace |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
23 |
|
public function setNamespace($namespace) |
39
|
|
|
{ |
40
|
23 |
|
$this->namespace = (string) $namespace; |
41
|
23 |
|
$this->namespaceVersion = null; |
42
|
23 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieves the namespace that prefixes all cache ids. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
1 |
|
public function getNamespace() |
50
|
|
|
{ |
51
|
1 |
|
return $this->namespace; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
690 |
|
public function fetch($id) |
58
|
|
|
{ |
59
|
690 |
|
return $this->doFetch($this->getNamespacedId($id)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
37 |
|
public function fetchMultiple(array $keys) |
66
|
|
|
{ |
67
|
37 |
|
if (empty($keys)) { |
68
|
11 |
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys |
72
|
26 |
|
$namespacedKeys = array_combine($keys, array_map([$this, 'getNamespacedId'], $keys)); |
73
|
26 |
|
$items = $this->doFetchMultiple($namespacedKeys); |
|
|
|
|
74
|
26 |
|
$foundItems = []; |
75
|
|
|
|
76
|
|
|
// no internal array function supports this sort of mapping: needs to be iterative |
77
|
|
|
// this filters and combines keys in one pass |
78
|
26 |
|
foreach ($namespacedKeys as $requestedKey => $namespacedKey) { |
79
|
26 |
|
if (! isset($items[$namespacedKey]) && ! array_key_exists($namespacedKey, $items)) { |
80
|
11 |
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
26 |
|
$foundItems[$requestedKey] = $items[$namespacedKey]; |
84
|
|
|
} |
85
|
|
|
|
86
|
26 |
|
return $foundItems; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
18 |
|
public function saveMultiple(array $keysAndValues, $lifetime = 0) |
93
|
|
|
{ |
94
|
18 |
|
$namespacedKeysAndValues = []; |
95
|
18 |
|
foreach ($keysAndValues as $key => $value) { |
96
|
18 |
|
$namespacedKeysAndValues[$this->getNamespacedId($key)] = $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
18 |
|
return $this->doSaveMultiple($namespacedKeysAndValues, $lifetime); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
771 |
|
public function contains($id) |
106
|
|
|
{ |
107
|
771 |
|
return $this->doContains($this->getNamespacedId($id)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
810 |
|
public function save($id, $data, $lifeTime = 0) |
114
|
|
|
{ |
115
|
810 |
|
return $this->doSave($this->getNamespacedId($id), $data, $lifeTime); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
13 |
|
public function deleteMultiple(array $keys) |
122
|
|
|
{ |
123
|
13 |
|
return $this->doDeleteMultiple(array_map([$this, 'getNamespacedId'], $keys)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
480 |
|
public function delete($id) |
130
|
|
|
{ |
131
|
480 |
|
return $this->doDelete($this->getNamespacedId($id)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
13 |
|
public function getStats() |
138
|
|
|
{ |
139
|
13 |
|
return $this->doGetStats(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
24 |
|
public function flushAll() |
146
|
|
|
{ |
147
|
24 |
|
return $this->doFlush(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
71 |
|
public function deleteAll() |
154
|
|
|
{ |
155
|
71 |
|
$namespaceCacheKey = $this->getNamespaceCacheKey(); |
156
|
71 |
|
$namespaceVersion = $this->getNamespaceVersion() + 1; |
157
|
|
|
|
158
|
71 |
|
if ($this->doSave($namespaceCacheKey, $namespaceVersion)) { |
159
|
70 |
|
$this->namespaceVersion = $namespaceVersion; |
160
|
|
|
|
161
|
70 |
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Prefixes the passed id with the configured namespace value. |
169
|
|
|
* |
170
|
|
|
* @param string $id The id to namespace. |
171
|
|
|
* |
172
|
|
|
* @return string The namespaced id. |
173
|
|
|
*/ |
174
|
856 |
|
private function getNamespacedId(string $id) : string |
175
|
|
|
{ |
176
|
856 |
|
$namespaceVersion = $this->getNamespaceVersion(); |
177
|
|
|
|
178
|
856 |
|
return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns the namespace cache key. |
183
|
|
|
*/ |
184
|
857 |
|
private function getNamespaceCacheKey() : string |
185
|
|
|
{ |
186
|
857 |
|
return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the namespace version. |
191
|
|
|
*/ |
192
|
857 |
|
private function getNamespaceVersion() : int |
193
|
|
|
{ |
194
|
857 |
|
if ($this->namespaceVersion !== null) { |
195
|
837 |
|
return $this->namespaceVersion; |
196
|
|
|
} |
197
|
|
|
|
198
|
857 |
|
$namespaceCacheKey = $this->getNamespaceCacheKey(); |
199
|
857 |
|
$this->namespaceVersion = (int) $this->doFetch($namespaceCacheKey) ?: 1; |
200
|
|
|
|
201
|
857 |
|
return $this->namespaceVersion; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it. |
206
|
|
|
* |
207
|
|
|
* @param array $keys Array of keys to retrieve from cache |
208
|
|
|
* |
209
|
|
|
* @return array Array of values retrieved for the given keys. |
210
|
|
|
*/ |
211
|
17 |
|
protected function doFetchMultiple(array $keys) |
212
|
|
|
{ |
213
|
17 |
|
$returnValues = []; |
214
|
|
|
|
215
|
17 |
|
foreach ($keys as $key) { |
216
|
17 |
|
$item = $this->doFetch($key); |
217
|
17 |
|
if ($item === false && ! $this->doContains($key)) { |
218
|
9 |
|
continue; |
219
|
|
|
} |
220
|
|
|
|
221
|
17 |
|
$returnValues[$key] = $item; |
222
|
|
|
} |
223
|
|
|
|
224
|
17 |
|
return $returnValues; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Fetches an entry from the cache. |
229
|
|
|
* |
230
|
|
|
* @param string $id The id of the cache entry to fetch. |
231
|
|
|
* |
232
|
|
|
* @return mixed|false The cached data or FALSE, if no cache entry exists for the given id. |
233
|
|
|
*/ |
234
|
|
|
abstract protected function doFetch($id); |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Tests if an entry exists in the cache. |
238
|
|
|
* |
239
|
|
|
* @param string $id The cache id of the entry to check for. |
240
|
|
|
* |
241
|
|
|
* @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise. |
242
|
|
|
*/ |
243
|
|
|
abstract protected function doContains($id); |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Default implementation of doSaveMultiple. Each driver that supports multi-put should override it. |
247
|
|
|
* |
248
|
|
|
* @param array $keysAndValues Array of keys and values to save in cache |
249
|
|
|
* @param int $lifetime The lifetime. If != 0, sets a specific lifetime for these |
250
|
|
|
* cache entries (0 => infinite lifeTime). |
251
|
|
|
* |
252
|
|
|
* @return bool TRUE if the operation was successful, FALSE if it wasn't. |
253
|
|
|
*/ |
254
|
11 |
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
255
|
|
|
{ |
256
|
11 |
|
$success = true; |
257
|
|
|
|
258
|
11 |
|
foreach ($keysAndValues as $key => $value) { |
259
|
11 |
|
if ($this->doSave($key, $value, $lifetime)) { |
260
|
11 |
|
continue; |
261
|
|
|
} |
262
|
|
|
|
263
|
2 |
|
$success = false; |
264
|
|
|
} |
265
|
|
|
|
266
|
11 |
|
return $success; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Puts data into the cache. |
271
|
|
|
* |
272
|
|
|
* @param string $id The cache id. |
273
|
|
|
* @param string $data The cache entry/data. |
274
|
|
|
* @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this |
275
|
|
|
* cache entry (0 => infinite lifeTime). |
276
|
|
|
* |
277
|
|
|
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise. |
278
|
|
|
*/ |
279
|
|
|
abstract protected function doSave($id, $data, $lifeTime = 0); |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Default implementation of doDeleteMultiple. Each driver that supports multi-delete should override it. |
283
|
|
|
* |
284
|
|
|
* @param array $keys Array of keys to delete from cache |
285
|
|
|
* |
286
|
|
|
* @return bool TRUE if the operation was successful, FALSE if it wasn't |
287
|
|
|
*/ |
288
|
8 |
|
protected function doDeleteMultiple(array $keys) |
289
|
|
|
{ |
290
|
8 |
|
$success = true; |
291
|
|
|
|
292
|
8 |
|
foreach ($keys as $key) { |
293
|
8 |
|
if ($this->doDelete($key)) { |
294
|
8 |
|
continue; |
295
|
|
|
} |
296
|
|
|
|
297
|
1 |
|
$success = false; |
298
|
|
|
} |
299
|
|
|
|
300
|
8 |
|
return $success; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Deletes a cache entry. |
305
|
|
|
* |
306
|
|
|
* @param string $id The cache id. |
307
|
|
|
* |
308
|
|
|
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
309
|
|
|
*/ |
310
|
|
|
abstract protected function doDelete($id); |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Flushes all cache entries. |
314
|
|
|
* |
315
|
|
|
* @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise. |
316
|
|
|
*/ |
317
|
|
|
abstract protected function doFlush(); |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Retrieves cached information from the data store. |
321
|
|
|
* |
322
|
|
|
* @return array|null An associative array with server's statistics if available, NULL otherwise. |
323
|
|
|
*/ |
324
|
|
|
abstract protected function doGetStats(); |
325
|
|
|
} |
326
|
|
|
|