vendor/api-platform/core/src/Cache/CachedTrait.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Cache;
  12. use Psr\Cache\CacheException;
  13. use Psr\Cache\CacheItemPoolInterface;
  14. /**
  15.  * @internal
  16.  */
  17. trait CachedTrait
  18. {
  19.     /** @var CacheItemPoolInterface */
  20.     private $cacheItemPool;
  21.     private $localCache = [];
  22.     private function getCached(string $cacheKey, callable $getValue)
  23.     {
  24.         if (\array_key_exists($cacheKey$this->localCache)) {
  25.             return $this->localCache[$cacheKey];
  26.         }
  27.         try {
  28.             $cacheItem $this->cacheItemPool->getItem($cacheKey);
  29.         } catch (CacheException $e) {
  30.             return $this->localCache[$cacheKey] = $getValue();
  31.         }
  32.         if ($cacheItem->isHit()) {
  33.             return $this->localCache[$cacheKey] = $cacheItem->get();
  34.         }
  35.         $value $getValue();
  36.         $cacheItem->set($value);
  37.         $this->cacheItemPool->save($cacheItem);
  38.         return $this->localCache[$cacheKey] = $value;
  39.     }
  40. }