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

  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(Request $requestSalesChannelContext $context): Response
  47.     {
  48.         $articleId $request->attributes->get('articleId');
  49.         $key $this->generateKey($articleId$context);
  50.         $value $this->cache->get($key, function (ItemInterface $item) use ($articleId$request$context) {
  51.             $response $this->decorated->detailAction($request$context);
  52.             $item->tag($this->generateTags($articleId));
  53.             return CacheValueCompressor::compress($response);
  54.         });
  55.         return CacheValueCompressor::uncompress($value);
  56.     }
  57.     private function generateKey(string $articleIdSalesChannelContext $context): string
  58.     {
  59.         $parts = [
  60.             $this->generator->getSalesChannelContextHash($context),
  61.         ];
  62.         return self::buildName($articleId) . '-' md5(JsonFieldSerializer::encodeJson($parts));
  63.     }
  64.     /**
  65.      * @return array<string>
  66.      */
  67.     private function generateTags(string $articleId): array
  68.     {
  69.         $tags array_merge(
  70.             $this->tracer->get(self::buildName($articleId)),
  71.             [self::buildName($articleId)]
  72.         );
  73.         return array_unique(array_filter($tags));
  74.     }
  75. }