custom/plugins/NetzpBlog6/src/Controller/StoreApi/BlogPost/CachedBlogPostRoute.php line 53

  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Controller\StoreApi\BlogPost;
  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 CachedBlogPostRoute extends AbstractBlogPostRoute
  13. {
  14.     private array $states = [];
  15.     public function __construct(private readonly AbstractBlogPostRoute $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(): AbstractBlogPostRoute
  23.     {
  24.         return $this->decorated;
  25.     }
  26.     #[Route(path'/store-api/blogpost/{postId}'name'store-api.s_plugin_netzp_blog.load'methods: ['GET''POST'], defaults: ['_routeScope' => ['store-api']])]
  27.     public function load(string $postIdCriteria $criteriaSalesChannelContext $context): BlogPostRouteResponse
  28.     {
  29.         if ($context->hasState(...$this->states)) {
  30.             return $this->getDecorated()->load($postId$criteria$context);
  31.         }
  32.         $item $this->cache->getItem(
  33.             $this->generateKey($postId$context$criteria)
  34.         );
  35.         try {
  36.             if ($item->isHit() && $item->get()) {
  37.                 return CacheCompressor::uncompress($item);
  38.             }
  39.         }
  40.         catch (\Throwable $e) {
  41.             $this->logger->error($e->getMessage());
  42.         }
  43.         $name self::buildName($postId);
  44.         $response $this->tracer->trace($name, fn() => $this->getDecorated()->load($postId$criteria$context));
  45.         $item CacheCompressor::compress($item$response);
  46.         $item->tag(array_merge(
  47.             $this->tracer->get(self::buildName($postId)),
  48.             [self::buildName($postId)]
  49.         ));
  50.         $this->cache->save($item);
  51.         return $response;
  52.     }
  53.     public static function buildName(string $postId): string
  54.     {
  55.         return 'blog-post-route-' $postId;
  56.     }
  57.     private function generateKey(string $postId,
  58.                                  SalesChannelContext $context,
  59.                                  Criteria $criteria): string
  60.     {
  61.         $customerId $context->getCustomer()?->getId();
  62.         $parts = [
  63.             self::buildName($postId),
  64.             $this->generator->getCriteriaHash($criteria),
  65.             $this->generator->getSalesChannelContextHash($context)
  66.         ];
  67.         if($customerId != null) {
  68.             $parts[] = $customerId;
  69.         }
  70.         return md5(Json::encode($parts));
  71.     }
  72. }