13
13
14
14
use Psr \Cache \CacheItemInterface ;
15
15
use Psr \Log \LoggerAwareInterface ;
16
- use Psr \Log \LoggerAwareTrait ;
17
16
use Psr \Log \LoggerInterface ;
18
17
use Symfony \Component \Cache \CacheItem ;
19
18
use Symfony \Component \Cache \Exception \InvalidArgumentException ;
19
+ use Symfony \Component \Cache \Traits \AbstractTrait ;
20
20
21
21
/**
22
22
* @author Nicolas Grekas <[email protected] >
23
23
*/
24
24
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
25
25
{
26
- use LoggerAwareTrait ;
26
+ use AbstractTrait ;
27
27
28
28
private static $ apcuSupported ;
29
29
private static $ phpFilesSupported ;
30
30
31
- private $ namespace ;
32
- private $ deferred = array ();
33
31
private $ createCacheItem ;
34
32
private $ mergeByLifetime ;
35
33
36
- /**
37
- * @var int|null The maximum length to enforce for identifiers or null when no limit applies
38
- */
39
- protected $ maxIdLength ;
40
-
41
34
protected function __construct ($ namespace = '' , $ defaultLifetime = 0 )
42
35
{
43
36
$ this ->namespace = '' === $ namespace ? '' : $ this ->getId ($ namespace ).': ' ;
@@ -130,52 +123,6 @@ public static function createConnection($dsn, array $options = array())
130
123
throw new InvalidArgumentException (sprintf ('Unsupported DSN: %s. ' , $ dsn ));
131
124
}
132
125
133
- /**
134
- * Fetches several cache items.
135
- *
136
- * @param array $ids The cache identifiers to fetch
137
- *
138
- * @return array|\Traversable The corresponding values found in the cache
139
- */
140
- abstract protected function doFetch (array $ ids );
141
-
142
- /**
143
- * Confirms if the cache contains specified cache item.
144
- *
145
- * @param string $id The identifier for which to check existence
146
- *
147
- * @return bool True if item exists in the cache, false otherwise
148
- */
149
- abstract protected function doHave ($ id );
150
-
151
- /**
152
- * Deletes all items in the pool.
153
- *
154
- * @param string The prefix used for all identifiers managed by this pool
155
- *
156
- * @return bool True if the pool was successfully cleared, false otherwise
157
- */
158
- abstract protected function doClear ($ namespace );
159
-
160
- /**
161
- * Removes multiple items from the pool.
162
- *
163
- * @param array $ids An array of identifiers that should be removed from the pool
164
- *
165
- * @return bool True if the items were successfully removed, false otherwise
166
- */
167
- abstract protected function doDelete (array $ ids );
168
-
169
- /**
170
- * Persists several cache items immediately.
171
- *
172
- * @param array $values The values to cache, indexed by their cache identifier
173
- * @param int $lifetime The lifetime of the cached values, 0 for persisting until manual cleaning
174
- *
175
- * @return array|bool The identifiers that failed to be cached or a boolean stating if caching succeeded or not
176
- */
177
- abstract protected function doSave (array $ values , $ lifetime );
178
-
179
126
/**
180
127
* {@inheritdoc}
181
128
*/
@@ -225,87 +172,6 @@ public function getItems(array $keys = array())
225
172
return $ this ->generateItems ($ items , $ ids );
226
173
}
227
174
228
- /**
229
- * {@inheritdoc}
230
- */
231
- public function hasItem ($ key )
232
- {
233
- $ id = $ this ->getId ($ key );
234
-
235
- if (isset ($ this ->deferred [$ key ])) {
236
- $ this ->commit ();
237
- }
238
-
239
- try {
240
- return $ this ->doHave ($ id );
241
- } catch (\Exception $ e ) {
242
- CacheItem::log ($ this ->logger , 'Failed to check if key "{key}" is cached ' , array ('key ' => $ key , 'exception ' => $ e ));
243
-
244
- return false ;
245
- }
246
- }
247
-
248
- /**
249
- * {@inheritdoc}
250
- */
251
- public function clear ()
252
- {
253
- $ this ->deferred = array ();
254
-
255
- try {
256
- return $ this ->doClear ($ this ->namespace );
257
- } catch (\Exception $ e ) {
258
- CacheItem::log ($ this ->logger , 'Failed to clear the cache ' , array ('exception ' => $ e ));
259
-
260
- return false ;
261
- }
262
- }
263
-
264
- /**
265
- * {@inheritdoc}
266
- */
267
- public function deleteItem ($ key )
268
- {
269
- return $ this ->deleteItems (array ($ key ));
270
- }
271
-
272
- /**
273
- * {@inheritdoc}
274
- */
275
- public function deleteItems (array $ keys )
276
- {
277
- $ ids = array ();
278
-
279
- foreach ($ keys as $ key ) {
280
- $ ids [$ key ] = $ this ->getId ($ key );
281
- unset($ this ->deferred [$ key ]);
282
- }
283
-
284
- try {
285
- if ($ this ->doDelete ($ ids )) {
286
- return true ;
287
- }
288
- } catch (\Exception $ e ) {
289
- }
290
-
291
- $ ok = true ;
292
-
293
- // When bulk-delete failed, retry each item individually
294
- foreach ($ ids as $ key => $ id ) {
295
- try {
296
- $ e = null ;
297
- if ($ this ->doDelete (array ($ id ))) {
298
- continue ;
299
- }
300
- } catch (\Exception $ e ) {
301
- }
302
- CacheItem::log ($ this ->logger , 'Failed to delete key "{key}" ' , array ('key ' => $ key , 'exception ' => $ e ));
303
- $ ok = false ;
304
- }
305
-
306
- return $ ok ;
307
- }
308
-
309
175
/**
310
176
* {@inheritdoc}
311
177
*/
@@ -394,47 +260,6 @@ public function __destruct()
394
260
}
395
261
}
396
262
397
- /**
398
- * Like the native unserialize() function but throws an exception if anything goes wrong.
399
- *
400
- * @param string $value
401
- *
402
- * @return mixed
403
- *
404
- * @throws \Exception
405
- */
406
- protected static function unserialize ($ value )
407
- {
408
- if ('b:0; ' === $ value ) {
409
- return false ;
410
- }
411
- $ unserializeCallbackHandler = ini_set ('unserialize_callback_func ' , __CLASS__ .'::handleUnserializeCallback ' );
412
- try {
413
- if (false !== $ value = unserialize ($ value )) {
414
- return $ value ;
415
- }
416
- throw new \DomainException ('Failed to unserialize cached value ' );
417
- } catch (\Error $ e ) {
418
- throw new \ErrorException ($ e ->getMessage (), $ e ->getCode (), E_ERROR , $ e ->getFile (), $ e ->getLine ());
419
- } finally {
420
- ini_set ('unserialize_callback_func ' , $ unserializeCallbackHandler );
421
- }
422
- }
423
-
424
- private function getId ($ key )
425
- {
426
- CacheItem::validateKey ($ key );
427
-
428
- if (null === $ this ->maxIdLength ) {
429
- return $ this ->namespace .$ key ;
430
- }
431
- if (strlen ($ id = $ this ->namespace .$ key ) > $ this ->maxIdLength ) {
432
- $ id = $ this ->namespace .substr_replace (base64_encode (hash ('sha256 ' , $ key , true )), ': ' , -22 );
433
- }
434
-
435
- return $ id ;
436
- }
437
-
438
263
private function generateItems ($ items , &$ keys )
439
264
{
440
265
$ f = $ this ->createCacheItem ;
@@ -453,12 +278,4 @@ private function generateItems($items, &$keys)
453
278
yield $ key => $ f ($ key , null , false );
454
279
}
455
280
}
456
-
457
- /**
458
- * @internal
459
- */
460
- public static function handleUnserializeCallback ($ class )
461
- {
462
- throw new \DomainException ('Class not found: ' .$ class );
463
- }
464
281
}
0 commit comments