vendor/symfony/cache/Traits/AbstractAdapterTrait.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Psr\Cache\CacheItemInterface;
  12. use Symfony\Component\Cache\CacheItem;
  13. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  14. /**
  15.  * @author Nicolas Grekas <p@tchwork.com>
  16.  *
  17.  * @internal
  18.  */
  19. trait AbstractAdapterTrait
  20. {
  21.     use AbstractTrait;
  22.     /**
  23.      * @var \Closure needs to be set by class, signature is function(string <key>, mixed <value>, bool <isHit>)
  24.      */
  25.     private $createCacheItem;
  26.     /**
  27.      * @var \Closure needs to be set by class, signature is function(array <deferred>, string <namespace>, array <&expiredIds>)
  28.      */
  29.     private $mergeByLifetime;
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function getItem($key)
  34.     {
  35.         if ($this->deferred) {
  36.             $this->commit();
  37.         }
  38.         $id $this->getId($key);
  39.         $f $this->createCacheItem;
  40.         $isHit false;
  41.         $value null;
  42.         try {
  43.             foreach ($this->doFetch([$id]) as $value) {
  44.                 $isHit true;
  45.             }
  46.             return $f($key$value$isHit);
  47.         } catch (\Exception $e) {
  48.             CacheItem::log($this->logger'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key'exception' => $e]);
  49.         }
  50.         return $f($keynullfalse);
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function getItems(array $keys = [])
  56.     {
  57.         if ($this->deferred) {
  58.             $this->commit();
  59.         }
  60.         $ids = [];
  61.         foreach ($keys as $key) {
  62.             $ids[] = $this->getId($key);
  63.         }
  64.         try {
  65.             $items $this->doFetch($ids);
  66.         } catch (\Exception $e) {
  67.             CacheItem::log($this->logger'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys'exception' => $e]);
  68.             $items = [];
  69.         }
  70.         $ids array_combine($ids$keys);
  71.         return $this->generateItems($items$ids);
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      *
  76.      * @return bool
  77.      */
  78.     public function save(CacheItemInterface $item)
  79.     {
  80.         if (!$item instanceof CacheItem) {
  81.             return false;
  82.         }
  83.         $this->deferred[$item->getKey()] = $item;
  84.         return $this->commit();
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      *
  89.      * @return bool
  90.      */
  91.     public function saveDeferred(CacheItemInterface $item)
  92.     {
  93.         if (!$item instanceof CacheItem) {
  94.             return false;
  95.         }
  96.         $this->deferred[$item->getKey()] = $item;
  97.         return true;
  98.     }
  99.     /**
  100.      * @return array
  101.      */
  102.     public function __sleep()
  103.     {
  104.         throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  105.     }
  106.     public function __wakeup()
  107.     {
  108.         throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  109.     }
  110.     public function __destruct()
  111.     {
  112.         if ($this->deferred) {
  113.             $this->commit();
  114.         }
  115.     }
  116.     private function generateItems(iterable $items, array &$keys): iterable
  117.     {
  118.         $f $this->createCacheItem;
  119.         try {
  120.             foreach ($items as $id => $value) {
  121.                 if (!isset($keys[$id])) {
  122.                     throw new InvalidArgumentException(sprintf('Could not match value id "%s" to keys "%s".'$idimplode('", "'$keys)));
  123.                 }
  124.                 $key $keys[$id];
  125.                 unset($keys[$id]);
  126.                 yield $key => $f($key$valuetrue);
  127.             }
  128.         } catch (\Exception $e) {
  129.             CacheItem::log($this->logger'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
  130.         }
  131.         foreach ($keys as $key) {
  132.             yield $key => $f($keynullfalse);
  133.         }
  134.     }
  135. }