vendor/symfony/cache/Traits/ApcuTrait.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 Symfony\Component\Cache\CacheItem;
  12. use Symfony\Component\Cache\Exception\CacheException;
  13. /**
  14.  * @author Nicolas Grekas <p@tchwork.com>
  15.  *
  16.  * @internal
  17.  */
  18. trait ApcuTrait
  19. {
  20.     public static function isSupported()
  21.     {
  22.         return \function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN);
  23.     }
  24.     private function init(string $namespaceint $defaultLifetime, ?string $version)
  25.     {
  26.         if (!static::isSupported()) {
  27.             throw new CacheException('APCu is not enabled.');
  28.         }
  29.         if ('cli' === \PHP_SAPI) {
  30.             ini_set('apc.use_request_time'0);
  31.         }
  32.         parent::__construct($namespace$defaultLifetime);
  33.         if (null !== $version) {
  34.             CacheItem::validateKey($version);
  35.             if (!apcu_exists($version.'@'.$namespace)) {
  36.                 $this->doClear($namespace);
  37.                 apcu_add($version.'@'.$namespacenull);
  38.             }
  39.         }
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function doFetch(array $ids)
  45.     {
  46.         $unserializeCallbackHandler ini_set('unserialize_callback_func'__CLASS__.'::handleUnserializeCallback');
  47.         try {
  48.             $values = [];
  49.             $ids array_flip($ids);
  50.             foreach (apcu_fetch(array_keys($ids), $ok) ?: [] as $k => $v) {
  51.                 if (!isset($ids[$k])) {
  52.                     // work around https://github.com/krakjoe/apcu/issues/247
  53.                     $k key($ids);
  54.                 }
  55.                 unset($ids[$k]);
  56.                 if (null !== $v || $ok) {
  57.                     $values[$k] = $v;
  58.                 }
  59.             }
  60.             return $values;
  61.         } catch (\Error $e) {
  62.             throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR$e->getFile(), $e->getLine());
  63.         } finally {
  64.             ini_set('unserialize_callback_func'$unserializeCallbackHandler);
  65.         }
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     protected function doHave($id)
  71.     {
  72.         return apcu_exists($id);
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     protected function doClear($namespace)
  78.     {
  79.         return isset($namespace[0]) && class_exists(\APCuIterator::class, false) && ('cli' !== \PHP_SAPI || filter_var(ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOLEAN))
  80.             ? apcu_delete(new \APCuIterator(sprintf('/^%s/'preg_quote($namespace'/')), \APC_ITER_KEY))
  81.             : apcu_clear_cache();
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     protected function doDelete(array $ids)
  87.     {
  88.         foreach ($ids as $id) {
  89.             apcu_delete($id);
  90.         }
  91.         return true;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     protected function doSave(array $valuesint $lifetime)
  97.     {
  98.         try {
  99.             if (false === $failures apcu_store($valuesnull$lifetime)) {
  100.                 $failures $values;
  101.             }
  102.             return array_keys($failures);
  103.         } catch (\Throwable $e) {
  104.             if (=== \count($values)) {
  105.                 // Workaround https://github.com/krakjoe/apcu/issues/170
  106.                 apcu_delete(array_key_first($values));
  107.             }
  108.             throw $e;
  109.         }
  110.     }
  111. }