vendor/shopware/core/Content/Flow/Dispatching/CachedFlowLoader.php line 83

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Content\Flow\FlowEvents;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * @internal not intended for decoration or replacement
  11.  */
  12. class CachedFlowLoader extends AbstractFlowLoader implements EventSubscriberInterface
  13. {
  14.     public const KEY 'flow-loader';
  15.     private array $flows = [];
  16.     private AbstractFlowLoader $decorated;
  17.     private TagAwareAdapterInterface $cache;
  18.     private LoggerInterface $logger;
  19.     public function __construct(
  20.         AbstractFlowLoader $decorated,
  21.         TagAwareAdapterInterface $cache,
  22.         LoggerInterface $logger
  23.     ) {
  24.         $this->decorated $decorated;
  25.         $this->cache $cache;
  26.         $this->logger $logger;
  27.     }
  28.     /**
  29.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             FlowEvents::FLOW_WRITTEN_EVENT => 'invalidate',
  35.         ];
  36.     }
  37.     public function getDecorated(): AbstractFlowLoader
  38.     {
  39.         return $this->decorated;
  40.     }
  41.     public function load(): array
  42.     {
  43.         if (!empty($this->flows)) {
  44.             return $this->flows;
  45.         }
  46.         $item $this->cache->getItem(self::KEY);
  47.         try {
  48.             if ($item->isHit() && $item->get()) {
  49.                 $this->logger->info('cache-hit: ' self::KEY);
  50.                 return $this->flows CacheCompressor::uncompress($item);
  51.             }
  52.         } catch (\Throwable $e) {
  53.             $this->logger->error($e->getMessage());
  54.         }
  55.         $this->logger->info('cache-miss: ' self::KEY);
  56.         $flows $this->getDecorated()->load();
  57.         $item CacheCompressor::compress($item$flows);
  58.         $item->tag([self::KEY]);
  59.         $this->cache->save($item);
  60.         return $this->flows $flows;
  61.     }
  62.     public function invalidate(EntityWrittenEvent $event): void
  63.     {
  64.         $this->flows = [];
  65.         $this->cache->deleteItem(self::KEY);
  66.     }
  67. }