custom/plugins/SasBlogModule/src/Controller/CachedBlogController.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Controller;
  3. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  4. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  6. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Controller\StorefrontController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Cache\CacheInterface;
  13. use Symfony\Contracts\Cache\ItemInterface;
  14. /**
  15.  * Handle Cache for BlogController
  16.  *
  17.  * @Route(defaults={"_routeScope"={"storefront"}})
  18.  */
  19. class CachedBlogController extends StorefrontController
  20. {
  21.     private BlogController $decorated;
  22.     private CacheInterface $cache;
  23.     private EntityCacheKeyGenerator $generator;
  24.     /**
  25.      * @var AbstractCacheTracer<Response>
  26.      */
  27.     private AbstractCacheTracer $tracer;
  28.     public function __construct(
  29.         BlogController $decorated,
  30.         CacheInterface $cache,
  31.         EntityCacheKeyGenerator $generator,
  32.         AbstractCacheTracer $tracer
  33.     ) {
  34.         $this->decorated $decorated;
  35.         $this->cache $cache;
  36.         $this->generator $generator;
  37.         $this->tracer $tracer;
  38.     }
  39.     public static function buildName(string $articleId): string
  40.     {
  41.         return 'sas-blog-detail-' $articleId;
  42.     }
  43.     /**
  44.      * @Route("/sas_blog/{articleId}", name="sas.frontend.blog.detail", methods={"GET"})
  45.      */
  46.     public function detailAction(string $articleIdRequest $requestSalesChannelContext $context): Response
  47.     {
  48.         $key $this->generateKey($articleId$context);
  49.         $value $this->cache->get($key, function (ItemInterface $item) use ($articleId$request$context) {
  50.             $name self::buildName($articleId);
  51.             $response $this->tracer->trace($name, function () use ($articleId$request$context) {
  52.                 return $this->decorated->detailAction($articleId$request$context);
  53.             });
  54.             $item->tag($this->generateTags($articleId));
  55.             return CacheValueCompressor::compress($response);
  56.         });
  57.         return CacheValueCompressor::uncompress($value);
  58.     }
  59.     private function generateKey(string $articleIdSalesChannelContext $context): string
  60.     {
  61.         $parts = [
  62.             $this->generator->getSalesChannelContextHash($context),
  63.         ];
  64.         return self::buildName($articleId) . '-' md5(JsonFieldSerializer::encodeJson($parts));
  65.     }
  66.     /**
  67.      * @return array<string>
  68.      */
  69.     private function generateTags(string $articleId): array
  70.     {
  71.         $tags array_merge(
  72.             $this->tracer->get(self::buildName($articleId)),
  73.             [self::buildName($articleId)]
  74.         );
  75.         return array_unique(array_filter($tags));
  76.     }
  77. }