"Fossies" - the Fresh Open Source Software Archive

Member "Symfony/vendor/symfony/symfony/src/Symfony/Component/Cache/Simple/AbstractCache.php" (30 Mar 2020, 5883 Bytes) of package /linux/www/Symfony_Standard_Vendors_3.4.39.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractCache.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /*
    4  * This file is part of the Symfony package.
    5  *
    6  * (c) Fabien Potencier <fabien@symfony.com>
    7  *
    8  * For the full copyright and license information, please view the LICENSE
    9  * file that was distributed with this source code.
   10  */
   11 
   12 namespace Symfony\Component\Cache\Simple;
   13 
   14 use Psr\Log\LoggerAwareInterface;
   15 use Psr\SimpleCache\CacheInterface;
   16 use Symfony\Component\Cache\CacheItem;
   17 use Symfony\Component\Cache\Exception\InvalidArgumentException;
   18 use Symfony\Component\Cache\ResettableInterface;
   19 use Symfony\Component\Cache\Traits\AbstractTrait;
   20 
   21 /**
   22  * @author Nicolas Grekas <p@tchwork.com>
   23  */
   24 abstract class AbstractCache implements CacheInterface, LoggerAwareInterface, ResettableInterface
   25 {
   26     /**
   27      * @internal
   28      */
   29     const NS_SEPARATOR = ':';
   30 
   31     use AbstractTrait {
   32         deleteItems as private;
   33         AbstractTrait::deleteItem as delete;
   34         AbstractTrait::hasItem as has;
   35     }
   36 
   37     private $defaultLifetime;
   38 
   39     /**
   40      * @param string $namespace
   41      * @param int    $defaultLifetime
   42      */
   43     protected function __construct($namespace = '', $defaultLifetime = 0)
   44     {
   45         $this->defaultLifetime = max(0, (int) $defaultLifetime);
   46         $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
   47         if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
   48             throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));
   49         }
   50     }
   51 
   52     /**
   53      * {@inheritdoc}
   54      */
   55     public function get($key, $default = null)
   56     {
   57         $id = $this->getId($key);
   58 
   59         try {
   60             foreach ($this->doFetch([$id]) as $value) {
   61                 return $value;
   62             }
   63         } catch (\Exception $e) {
   64             CacheItem::log($this->logger, 'Failed to fetch key "{key}"', ['key' => $key, 'exception' => $e]);
   65         }
   66 
   67         return $default;
   68     }
   69 
   70     /**
   71      * {@inheritdoc}
   72      */
   73     public function set($key, $value, $ttl = null)
   74     {
   75         CacheItem::validateKey($key);
   76 
   77         return $this->setMultiple([$key => $value], $ttl);
   78     }
   79 
   80     /**
   81      * {@inheritdoc}
   82      */
   83     public function getMultiple($keys, $default = null)
   84     {
   85         if ($keys instanceof \Traversable) {
   86             $keys = iterator_to_array($keys, false);
   87         } elseif (!\is_array($keys)) {
   88             throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
   89         }
   90         $ids = [];
   91 
   92         foreach ($keys as $key) {
   93             $ids[] = $this->getId($key);
   94         }
   95         try {
   96             $values = $this->doFetch($ids);
   97         } catch (\Exception $e) {
   98             CacheItem::log($this->logger, 'Failed to fetch requested values', ['keys' => $keys, 'exception' => $e]);
   99             $values = [];
  100         }
  101         $ids = array_combine($ids, $keys);
  102 
  103         return $this->generateValues($values, $ids, $default);
  104     }
  105 
  106     /**
  107      * {@inheritdoc}
  108      */
  109     public function setMultiple($values, $ttl = null)
  110     {
  111         if (!\is_array($values) && !$values instanceof \Traversable) {
  112             throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given.', \is_object($values) ? \get_class($values) : \gettype($values)));
  113         }
  114         $valuesById = [];
  115 
  116         foreach ($values as $key => $value) {
  117             if (\is_int($key)) {
  118                 $key = (string) $key;
  119             }
  120             $valuesById[$this->getId($key)] = $value;
  121         }
  122         if (false === $ttl = $this->normalizeTtl($ttl)) {
  123             return $this->doDelete(array_keys($valuesById));
  124         }
  125 
  126         try {
  127             $e = $this->doSave($valuesById, $ttl);
  128         } catch (\Exception $e) {
  129         }
  130         if (true === $e || [] === $e) {
  131             return true;
  132         }
  133         $keys = [];
  134         foreach (\is_array($e) ? $e : array_keys($valuesById) as $id) {
  135             $keys[] = substr($id, \strlen($this->namespace));
  136         }
  137         CacheItem::log($this->logger, 'Failed to save values', ['keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null]);
  138 
  139         return false;
  140     }
  141 
  142     /**
  143      * {@inheritdoc}
  144      */
  145     public function deleteMultiple($keys)
  146     {
  147         if ($keys instanceof \Traversable) {
  148             $keys = iterator_to_array($keys, false);
  149         } elseif (!\is_array($keys)) {
  150             throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  151         }
  152 
  153         return $this->deleteItems($keys);
  154     }
  155 
  156     private function normalizeTtl($ttl)
  157     {
  158         if (null === $ttl) {
  159             return $this->defaultLifetime;
  160         }
  161         if ($ttl instanceof \DateInterval) {
  162             $ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
  163         }
  164         if (\is_int($ttl)) {
  165             return 0 < $ttl ? $ttl : false;
  166         }
  167 
  168         throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given.', \is_object($ttl) ? \get_class($ttl) : \gettype($ttl)));
  169     }
  170 
  171     private function generateValues($values, &$keys, $default)
  172     {
  173         try {
  174             foreach ($values as $id => $value) {
  175                 if (!isset($keys[$id])) {
  176                     $id = key($keys);
  177                 }
  178                 $key = $keys[$id];
  179                 unset($keys[$id]);
  180                 yield $key => $value;
  181             }
  182         } catch (\Exception $e) {
  183             CacheItem::log($this->logger, 'Failed to fetch requested values', ['keys' => array_values($keys), 'exception' => $e]);
  184         }
  185 
  186         foreach ($keys as $key) {
  187             yield $key => $default;
  188         }
  189     }
  190 }