custom/plugins/NetzpBlog6/src/Controller/StoreApi/BlogListing/CachedBlogListingRoute.php line 59

  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Controller\StoreApi\BlogListing;
  3. use Shopware\Core\Framework\Util\Json;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  9. use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  11. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  12. class CachedBlogListingRoute extends AbstractBlogListingRoute
  13. {
  14.     private array $states = [];
  15.     public function __construct(private readonly AbstractBlogListingRoute $decorated,
  16.                                 private readonly TagAwareAdapterInterface $cache,
  17.                                 private readonly EntityCacheKeyGenerator $generator,
  18.                                 private readonly AbstractCacheTracer $tracer,
  19.                                 private readonly LoggerInterface $logger)
  20.     {
  21.     }
  22.     public function getDecorated(): AbstractBlogListingRoute
  23.     {
  24.         return $this->decorated;
  25.     }
  26.     #[Route(path'/store-api/bloglisting/{navigationId?}'name'store-api.s_plugin_netzp_blog_listing.load'methods: ['GET''POST'], defaults: ['_routeScope' => ['store-api']])]
  27.     public function load(?string $navigationId,
  28.                          Criteria $criteria,
  29.                          SalesChannelContext $context,
  30.                          ?string $categoryId,
  31.                          ?string $authorId,
  32.                          ?array $tags,
  33.                          ?string $sortOrder): BlogListingRouteResponse
  34.     {
  35.         if ($context->hasState(...$this->states)) {
  36.             return $this->getDecorated()->load($navigationId$criteria$context$categoryId$authorId$tags$sortOrder);
  37.         }
  38.         $item $this->cache->getItem(
  39.             $this->generateKey($navigationId$context$criteria)
  40.         );
  41.         try {
  42.             if ($item->isHit() && $item->get()) {
  43.                 return CacheCompressor::uncompress($item);
  44.             }
  45.         }
  46.         catch (\Throwable $e) {
  47.             $this->logger->error($e->getMessage());
  48.         }
  49.         $name self::buildName($navigationId);
  50.         $response $this->tracer->trace($name, fn() => $this->getDecorated()->load($navigationId$criteria$context$categoryId$authorId$tags$sortOrder));
  51.         $item CacheCompressor::compress($item$response);
  52.         $item->tag(array_merge(
  53.             $this->tracer->get(self::buildName($navigationId)),
  54.             [
  55.                 self::buildName($navigationId),
  56.                 self::buildName('')
  57.             ]
  58.         ));
  59.         $this->cache->save($item);
  60.         return $response;
  61.     }
  62.     public static function buildName(string $navigationId): string
  63.     {
  64.         return 'blog-listing-route-' $navigationId;
  65.     }
  66.     private function generateKey(string $navigationId,
  67.                                  SalesChannelContext $context,
  68.                                  Criteria $criteria): string
  69.     {
  70.         $customerId $context->getCustomer()?->getId();
  71.         $parts = [
  72.             self::buildName($navigationId),
  73.             $this->generator->getCriteriaHash($criteria),
  74.             $this->generator->getSalesChannelContextHash($context),
  75.         ];
  76.         if($customerId != null) {
  77.             $parts[] = $customerId;
  78.         }
  79.         return md5(Json::encode($parts));
  80.     }
  81. }