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

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.         $id $this->getId($key);
  36.         if (isset($this->deferred[$key])) {
  37.             $this->commit();
  38.         }
  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.         $ids = [];
  58.         $commit false;
  59.         foreach ($keys as $key) {
  60.             $ids[] = $this->getId($key);
  61.             $commit $commit || isset($this->deferred[$key]);
  62.         }
  63.         if ($commit) {
  64.             $this->commit();
  65.         }
  66.         try {
  67.             $items $this->doFetch($ids);
  68.         } catch (\Exception $e) {
  69.             CacheItem::log($this->logger'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys'exception' => $e]);
  70.             $items = [];
  71.         }
  72.         $ids array_combine($ids$keys);
  73.         return $this->generateItems($items$ids);
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      *
  78.      * @return bool
  79.      */
  80.     public function save(CacheItemInterface $item)
  81.     {
  82.         if (!$item instanceof CacheItem) {
  83.             return false;
  84.         }
  85.         $this->deferred[$item->getKey()] = $item;
  86.         return $this->commit();
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      *
  91.      * @return bool
  92.      */
  93.     public function saveDeferred(CacheItemInterface $item)
  94.     {
  95.         if (!$item instanceof CacheItem) {
  96.             return false;
  97.         }
  98.         $this->deferred[$item->getKey()] = $item;
  99.         return true;
  100.     }
  101.     /**
  102.      * @return array
  103.      */
  104.     public function __sleep()
  105.     {
  106.         throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  107.     }
  108.     public function __wakeup()
  109.     {
  110.         throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  111.     }
  112.     public function __destruct()
  113.     {
  114.         if ($this->deferred) {
  115.             $this->commit();
  116.         }
  117.     }
  118.     private function generateItems(iterable $items, array &$keys): iterable
  119.     {
  120.         $f $this->createCacheItem;
  121.         try {
  122.             foreach ($items as $id => $value) {
  123.                 if (!isset($keys[$id])) {
  124.                     throw new InvalidArgumentException(sprintf('Could not match value id "%s" to keys "%s".'$idimplode('", "'$keys)));
  125.                 }
  126.                 $key $keys[$id];
  127.                 unset($keys[$id]);
  128.                 yield $key => $f($key$valuetrue);
  129.             }
  130.         } catch (\Exception $e) {
  131.             CacheItem::log($this->logger'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
  132.         }
  133.         foreach ($keys as $key) {
  134.             yield $key => $f($keynullfalse);
  135.         }
  136.     }
  137. }