vendor/pimcore/pimcore/lib/Cache/Pool/SymfonyAdapterProxy.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Cache\Pool;
  15. use Pimcore\Cache\Pool\Exception\InvalidArgumentException;
  16. use Symfony\Component\Cache\Adapter\AdapterInterface;
  17. use Symfony\Component\Cache\Adapter\TagAwareAdapter;
  18. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  19. use Symfony\Component\Cache\CacheItem;
  20. class SymfonyAdapterProxy extends AbstractCacheItemPool
  21. {
  22.     /**
  23.      * @var TagAwareAdapterInterface
  24.      */
  25.     protected $adapter;
  26.     /**
  27.      * @var \Closure
  28.      */
  29.     protected $transformItemClosure;
  30.     /**
  31.      * @param TagAwareAdapterInterface|AdapterInterface $adapter
  32.      */
  33.     public function __construct(AdapterInterface $adapter$defaultLifetime 0)
  34.     {
  35.         parent::__construct($defaultLifetime);
  36.         // auto-wrap adapter in tag aware adapter if the passed adapter is not tag aware
  37.         if (!($adapter instanceof TagAwareAdapterInterface)) {
  38.             $adapter = new TagAwareAdapter($adapter);
  39.         }
  40.         $this->adapter $adapter;
  41.     }
  42.     /**
  43.      * Fetches several cache items.
  44.      *
  45.      * @param array $ids The cache identifiers to fetch
  46.      *
  47.      * @return array|\Traversable The corresponding values found in the cache
  48.      */
  49.     protected function doFetch(array $ids)
  50.     {
  51.         if (empty($ids)) {
  52.             return;
  53.         }
  54.         $keys = [];
  55.         foreach ($ids as $id) {
  56.             $keys[$id] = $id;
  57.         }
  58.         /** @var CacheItem $item */
  59.         foreach ($this->adapter->getItems($ids) as $item) {
  60.             if ($item->isHit()) {
  61.                 $data $this->unserializeData($item->get());
  62.                 yield $item->getKey() => [
  63.                     'value' => $data,
  64.                     'tags' => []
  65.                 ];
  66.             }
  67.         }
  68.     }
  69.     /**
  70.      * Confirms if the cache contains specified cache item.
  71.      *
  72.      * @param string $id The identifier for which to check existence
  73.      *
  74.      * @return bool True if item exists in the cache, false otherwise
  75.      */
  76.     protected function doHave($id)
  77.     {
  78.         return $this->adapter->getItem($id)->isHit();
  79.     }
  80.     /**
  81.      * Deletes all items in the pool.
  82.      *
  83.      * @param string $namespace The prefix used for all identifiers managed by this pool
  84.      *
  85.      * @return bool True if the pool was successfully cleared, false otherwise
  86.      */
  87.     protected function doClear($namespace)
  88.     {
  89.         return $this->adapter->clear($namespace);
  90.     }
  91.     /**
  92.      * Removes multiple items from the pool.
  93.      *
  94.      * @param array $ids An array of identifiers that should be removed from the pool
  95.      *
  96.      * @return bool True if the items were successfully removed, false otherwise
  97.      */
  98.     protected function doDelete(array $ids)
  99.     {
  100.         return $this->adapter->deleteItems($ids);
  101.     }
  102.     /**
  103.      * Persists any deferred cache items.
  104.      *
  105.      * @return bool
  106.      *   True if all not-yet-saved items were successfully saved or there were none. False otherwise.
  107.      */
  108.     public function commit()
  109.     {
  110.         if (empty($this->deferred)) {
  111.             return true;
  112.         }
  113.         $keys array_keys($this->deferred);
  114.         $symfonyItems = [];
  115.         /** @var CacheItem $symfonyItem */
  116.         foreach ($this->adapter->getItems($keys) as $symfonyItem) {
  117.             $symfonyItems[$symfonyItem->getKey()] = $symfonyItem;
  118.         }
  119.         foreach ($keys as $key) {
  120.             $cacheItem $this->deferred[$key];
  121.             $symfonyItem $symfonyItems[$key];
  122.             unset($this->deferred[$key]);
  123.             unset($symfonyItems[$key]);
  124.             $this->transformItem($cacheItem$symfonyItem);
  125.             $this->adapter->saveDeferred($symfonyItem);
  126.         }
  127.         if (!empty($this->deferred) || !empty($symfonyItems)) {
  128.             $this->logger->error('Not all deferred cache items were processed', [
  129.                 'deferred' => array_keys($this->deferred),
  130.                 'symfony' => array_keys($symfonyItems)
  131.             ]);
  132.             return false;
  133.         }
  134.         return $this->adapter->commit();
  135.     }
  136.     /**
  137.      * @param PimcoreCacheItemInterface $cacheItem
  138.      * @param CacheItem $symfonyItem
  139.      */
  140.     protected function transformItem(PimcoreCacheItemInterface $cacheItemCacheItem $symfonyItem)
  141.     {
  142.         if (null === $this->transformItemClosure) {
  143.             $this->transformItemClosure = \Closure::bind(
  144.                 function (CacheItem $symfonyItem$data, array $tags$expiry) {
  145.                     $symfonyItem->value $data;
  146.                     $symfonyItem->expiry $expiry;
  147.                     if (property_exists($symfonyItem'tags')) {
  148.                         $symfonyItem->tags = [];
  149.                     }
  150.                     $symfonyItem->tag($tags);
  151.                 },
  152.                 null,
  153.                 CacheItem::class
  154.             );
  155.         }
  156.         $tags $cacheItem->getTags();
  157.         $closure $this->transformItemClosure;
  158.         $closure($symfonyItem$this->serializeData($cacheItem->get()), $tags$cacheItem->getExpiry());
  159.     }
  160.     /**
  161.      * Invalidates cached items using tags.
  162.      *
  163.      * @param string[] $tags An array of tags to invalidate
  164.      *
  165.      * @throws InvalidArgumentException When $tags is not valid
  166.      *
  167.      * @return bool True on success
  168.      */
  169.     protected function doInvalidateTags(array $tags)
  170.     {
  171.         return $this->adapter->invalidateTags($tags);
  172.     }
  173. }