vendor/shopware/platform/src/Core/System/SalesChannel/Context/CachedSalesChannelContextFactory.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\Context;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  8. class CachedSalesChannelContextFactory extends AbstractSalesChannelContextFactory
  9. {
  10.     public const ALL_TAG 'sales-channel-context';
  11.     private AbstractSalesChannelContextFactory $decorated;
  12.     private TagAwareAdapterInterface $cache;
  13.     /**
  14.      * @var AbstractCacheTracer<SalesChannelContext>
  15.      */
  16.     private AbstractCacheTracer $tracer;
  17.     private LoggerInterface $logger;
  18.     /**
  19.      * @param AbstractCacheTracer<SalesChannelContext> $tracer
  20.      */
  21.     public function __construct(
  22.         AbstractSalesChannelContextFactory $decorated,
  23.         TagAwareAdapterInterface $cache,
  24.         AbstractCacheTracer $tracer,
  25.         LoggerInterface $logger
  26.     ) {
  27.         $this->decorated $decorated;
  28.         $this->cache $cache;
  29.         $this->tracer $tracer;
  30.         $this->logger $logger;
  31.     }
  32.     public function getDecorated(): AbstractSalesChannelContextFactory
  33.     {
  34.         return $this->decorated;
  35.     }
  36.     public function create(string $tokenstring $salesChannelId, array $options = []): SalesChannelContext
  37.     {
  38.         $name self::buildName($salesChannelId);
  39.         if (!$this->isCacheable($options)) {
  40.             $this->logger->info('cache-miss: ' $name);
  41.             return $this->getDecorated()->create($token$salesChannelId$options);
  42.         }
  43.         ksort($options);
  44.         $key md5(implode('-', [
  45.             $name,
  46.             json_encode($options),
  47.         ]));
  48.         $item $this->cache->getItem($key);
  49.         try {
  50.             if ($item->isHit() && $item->get()) {
  51.                 $this->logger->info('cache-hit: ' $name);
  52.                 /** @var SalesChannelContext $context */
  53.                 $context CacheCompressor::uncompress($item);
  54.                 $context->assign(['token' => $token]);
  55.                 return $context;
  56.             }
  57.         } catch (\Throwable $e) {
  58.             $this->logger->error($e->getMessage());
  59.         }
  60.         $this->logger->info('cache-miss: ' $name);
  61.         $context $this->tracer->trace($name, function () use ($token$salesChannelId$options) {
  62.             return $this->getDecorated()->create($token$salesChannelId$options);
  63.         });
  64.         $keys array_unique(array_merge(
  65.             $this->tracer->get($name),
  66.             [$nameself::ALL_TAG]
  67.         ));
  68.         $item CacheCompressor::compress($item$context);
  69.         $item->tag($keys);
  70.         $this->cache->save($item);
  71.         return $context;
  72.     }
  73.     public static function buildName(string $salesChannelId): string
  74.     {
  75.         return 'context-factory-' $salesChannelId;
  76.     }
  77.     private function isCacheable(array $options): bool
  78.     {
  79.         return !isset($options[SalesChannelContextService::CUSTOMER_ID])
  80.             && !isset($options[SalesChannelContextService::BILLING_ADDRESS_ID])
  81.             && !isset($options[SalesChannelContextService::SHIPPING_ADDRESS_ID]);
  82.     }
  83. }