1313
1414use Psr \Cache \CacheItemInterface ;
1515use Psr \Log \LoggerAwareInterface ;
16- use Psr \Log \LoggerAwareTrait ;
1716use Psr \Log \LoggerInterface ;
1817use Symfony \Component \Cache \CacheItem ;
1918use Symfony \Component \Cache \Exception \InvalidArgumentException ;
19+ use Symfony \Component \Cache \Traits \AbstractTrait ;
2020
2121/**
2222 * @author Nicolas Grekas <p@tchwork.com>
2323 */
2424abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
2525{
26- use LoggerAwareTrait ;
26+ use AbstractTrait ;
2727
2828 private static $ apcuSupported ;
2929 private static $ phpFilesSupported ;
3030
31- private $ namespace ;
32- private $ deferred = array ();
3331 private $ createCacheItem ;
3432 private $ mergeByLifetime ;
3533
36- /**
37- * @var int|null The maximum length to enforce for identifiers or null when no limit applies
38- */
39- protected $ maxIdLength ;
40-
4134 protected function __construct ($ namespace = '' , $ defaultLifetime = 0 )
4235 {
4336 $ this ->namespace = '' === $ namespace ? '' : $ this ->getId ($ namespace ).': ' ;
@@ -130,52 +123,6 @@ public static function createConnection($dsn, array $options = array())
130123 throw new InvalidArgumentException (sprintf ('Unsupported DSN: %s. ' , $ dsn ));
131124 }
132125
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-
179126 /**
180127 * {@inheritdoc}
181128 */
@@ -225,87 +172,6 @@ public function getItems(array $keys = array())
225172 return $ this ->generateItems ($ items , $ ids );
226173 }
227174
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-
309175 /**
310176 * {@inheritdoc}
311177 */
@@ -394,47 +260,6 @@ public function __destruct()
394260 }
395261 }
396262
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-
438263 private function generateItems ($ items , &$ keys )
439264 {
440265 $ f = $ this ->createCacheItem ;
@@ -453,12 +278,4 @@ private function generateItems($items, &$keys)
453278 yield $ key => $ f ($ key , null , false );
454279 }
455280 }
456-
457- /**
458- * @internal
459- */
460- public static function handleUnserializeCallback ($ class )
461- {
462- throw new \DomainException ('Class not found: ' .$ class );
463- }
464281}
0 commit comments