vendor/store.shopware.com/netzpblog6/src/Controller/StoreApi/BlogPost/CachedBlogPostRoute.php line 103

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Controller\StoreApi\BlogPost;
  3. use OpenApi\Annotations as OA;
  4. use Shopware\Core\Framework\Routing\Annotation\Entity;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  10. use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  12. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  13. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  14. class CachedBlogPostRoute extends AbstractBlogPostRoute
  15. {
  16.     private AbstractBlogPostRoute $decorated;
  17.     private TagAwareAdapterInterface $cache;
  18.     private EntityCacheKeyGenerator $generator;
  19.     private AbstractCacheTracer $tracer;
  20.     private array $states;
  21.     private LoggerInterface $logger;
  22.     public function __construct(
  23.         AbstractBlogPostRoute $decorated,
  24.         TagAwareAdapterInterface $cache,
  25.         EntityCacheKeyGenerator $generator,
  26.         AbstractCacheTracer $tracer,
  27.         LoggerInterface $logger
  28.     ) {
  29.         $this->decorated $decorated;
  30.         $this->cache $cache;
  31.         $this->generator $generator;
  32.         $this->tracer $tracer;
  33.         $this->states = [];
  34.         $this->logger $logger;
  35.     }
  36.     public function getDecorated(): AbstractBlogPostRoute
  37.     {
  38.         return $this->decorated;
  39.     }
  40.     /**
  41.      * @Entity("s_plugin_netzp_blog")
  42.      * @OA\Post(
  43.      *      path="/blogpost/{postId}",
  44.      *      summary="This route can be used to load the s_plugin_netzp_blog",
  45.      *      operationId="readNetzpBlog",
  46.      *      tags={"Store API", "NetzpBlog"},
  47.      *      @OA\Parameter(name="Api-Basic-Parameters"),
  48.      *      @OA\Response(
  49.      *          response="200",
  50.      *          description="",
  51.      *          @OA\JsonContent(type="object",
  52.      *              @OA\Property(
  53.      *                  property="total",
  54.      *                  type="integer",
  55.      *                  description="Total amount"
  56.      *              ),
  57.      *              @OA\Property(
  58.      *                  property="aggregations",
  59.      *                  type="object",
  60.      *                  description="aggregation result"
  61.      *              )
  62.      *          )
  63.      *     )
  64.      * )
  65.      * @Route("/store-api/blogpost/{postId}", name="store-api.s_plugin_netzp_blog.load", methods={"GET", "POST"}, defaults={"_routeScope"={"store-api"}})
  66.      */
  67.     public function load(string $postIdCriteria $criteriaSalesChannelContext $context): BlogPostRouteResponse
  68.     {
  69.         // The context is provided with a state where the route cannot be cached
  70.         if ($context->hasState(...$this->states)) {
  71.             return $this->getDecorated()->load($postId$criteria$context);
  72.         }
  73.         // Fetch item from the cache pool
  74.         $item $this->cache->getItem(
  75.             $this->generateKey($postId$context$criteria)
  76.         );
  77.         try {
  78.             if ($item->isHit() && $item->get()) {
  79.                 // Use cache compressor to uncompress the cache value
  80.                 return CacheCompressor::uncompress($item);
  81.             }
  82.         } catch (\Throwable $e) {
  83.             // Something went wrong when uncompress the cache item - we log the error and continue to overwrite the invalid cache item
  84.             $this->logger->error($e->getMessage());
  85.         }
  86.         $name self::buildName($postId);
  87.         // start tracing of nested cache tags and system config keys
  88.         $response $this->tracer->trace($name, function () use ($criteria$context$postId) {
  89.             return $this->getDecorated()->load($postId$criteria$context);
  90.         });
  91.         // compress cache content to reduce cache size
  92.         $item CacheCompressor::compress($item$response);
  93.         $item->tag(array_merge(
  94.         // get traced tags and configs
  95.             $this->tracer->get(self::buildName($postId)),
  96.             [self::buildName($postId)]
  97.         ));
  98.         $this->cache->save($item);
  99.         return $response;
  100.     }
  101.     public static function buildName(string $postId): string
  102.     {
  103.         return 'blog-post-route-' $postId;
  104.     }
  105.     private function generateKey(string $postIdSalesChannelContext $contextCriteria $criteria): string
  106.     {
  107.         $parts = [
  108.             self::buildName($postId),
  109.             $this->generator->getCriteriaHash($criteria),
  110.             $this->generator->getSalesChannelContextHash($context),
  111.         ];
  112.         return md5(JsonFieldSerializer::encodeJson($parts));
  113.     }
  114. }