vendor/store.shopware.com/netzpblog6/src/Controller/StoreApi/BlogListing/CachedBlogListingRoute.php line 103

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Controller\StoreApi\BlogListing;
  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 CachedBlogListingRoute extends AbstractBlogListingRoute
  15. {
  16.     private AbstractBlogListingRoute $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.         AbstractBlogListingRoute $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(): AbstractBlogListingRoute
  37.     {
  38.         return $this->decorated;
  39.     }
  40.     /**
  41.      * @Entity("s_plugin_netzp_blog")
  42.      * @OA\Post(
  43.      *      path="/bloglisting",
  44.      *      summary="This route can be used to load a blog listing",
  45.      *      operationId="readNetzpBlogListing",
  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/bloglisting/{navigationId?}", name="store-api.s_plugin_netzp_blog_listing.load", methods={"GET", "POST"}, defaults={"_routeScope"={"store-api"}})
  66.      */
  67.     public function load($navigationIdCriteria $criteriaSalesChannelContext $context,
  68.         ?string $categoryId, ?string $authorId, ?array $tags, ?string $sortOrder): BlogListingRouteResponse
  69.     {
  70.         // The context is provided with a state where the route cannot be cached
  71.         if ($context->hasState(...$this->states)) {
  72.             return $this->getDecorated()->load($navigationId$criteria$context$categoryId$authorId$tags$sortOrder);
  73.         }
  74.         // Fetch item from the cache pool
  75.         $item $this->cache->getItem(
  76.             $this->generateKey($navigationId$context$criteria)
  77.         );
  78.         try {
  79.             if ($item->isHit() && $item->get()) {
  80.                 // Use cache compressor to uncompress the cache value
  81.                 return CacheCompressor::uncompress($item);
  82.             }
  83.         } catch (\Throwable $e) {
  84.             // Something went wrong when uncompress the cache item - we log the error and continue to overwrite the invalid cache item
  85.             $this->logger->error($e->getMessage());
  86.         }
  87.         $name self::buildName($navigationId);
  88.         // start tracing of nested cache tags and system config keys
  89.         $response $this->tracer->trace($name, function () use ($navigationId$criteria$context$categoryId$authorId$tags$sortOrder) {
  90.             return $this->getDecorated()->load($navigationId$criteria$context$categoryId$authorId$tags$sortOrder);
  91.         });
  92.         // compress cache content to reduce cache size
  93.         $item CacheCompressor::compress($item$response);
  94.         $item->tag(array_merge(
  95.         // get traced tags and configs
  96.             $this->tracer->get(self::buildName($navigationId)),
  97.             [self::buildName($navigationId), self::buildName('')]
  98.         ));
  99.         $this->cache->save($item);
  100.         return $response;
  101.     }
  102.     public static function buildName($navigationId): string
  103.     {
  104.         return 'blog-listing-route-' $navigationId;
  105.     }
  106.     private function generateKey($navigationIdSalesChannelContext $contextCriteria $criteria): string
  107.     {
  108.         $parts = [
  109.             self::buildName($navigationId),
  110.             // generate a hash for the route criteria
  111.             $this->generator->getCriteriaHash($criteria),
  112.             // generate a hash for the current context
  113.             $this->generator->getSalesChannelContextHash($context),
  114.         ];
  115.         return md5(JsonFieldSerializer::encodeJson($parts));
  116.     }
  117. }