<?php declare(strict_types=1);
namespace NetzpBlog6\Controller\StoreApi\BlogPost;
use OpenApi\Annotations as OA;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Symfony\Component\Routing\Annotation\Route;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
class CachedBlogPostRoute extends AbstractBlogPostRoute
{
private AbstractBlogPostRoute $decorated;
private TagAwareAdapterInterface $cache;
private EntityCacheKeyGenerator $generator;
private AbstractCacheTracer $tracer;
private array $states;
private LoggerInterface $logger;
public function __construct(
AbstractBlogPostRoute $decorated,
TagAwareAdapterInterface $cache,
EntityCacheKeyGenerator $generator,
AbstractCacheTracer $tracer,
LoggerInterface $logger
) {
$this->decorated = $decorated;
$this->cache = $cache;
$this->generator = $generator;
$this->tracer = $tracer;
$this->states = [];
$this->logger = $logger;
}
public function getDecorated(): AbstractBlogPostRoute
{
return $this->decorated;
}
/**
* @Entity("s_plugin_netzp_blog")
* @OA\Post(
* path="/blogpost/{postId}",
* summary="This route can be used to load the s_plugin_netzp_blog",
* operationId="readNetzpBlog",
* tags={"Store API", "NetzpBlog"},
* @OA\Parameter(name="Api-Basic-Parameters"),
* @OA\Response(
* response="200",
* description="",
* @OA\JsonContent(type="object",
* @OA\Property(
* property="total",
* type="integer",
* description="Total amount"
* ),
* @OA\Property(
* property="aggregations",
* type="object",
* description="aggregation result"
* )
* )
* )
* )
* @Route("/store-api/blogpost/{postId}", name="store-api.s_plugin_netzp_blog.load", methods={"GET", "POST"}, defaults={"_routeScope"={"store-api"}})
*/
public function load(string $postId, Criteria $criteria, SalesChannelContext $context): BlogPostRouteResponse
{
// The context is provided with a state where the route cannot be cached
if ($context->hasState(...$this->states)) {
return $this->getDecorated()->load($postId, $criteria, $context);
}
// Fetch item from the cache pool
$item = $this->cache->getItem(
$this->generateKey($postId, $context, $criteria)
);
try {
if ($item->isHit() && $item->get()) {
// Use cache compressor to uncompress the cache value
return CacheCompressor::uncompress($item);
}
} catch (\Throwable $e) {
// Something went wrong when uncompress the cache item - we log the error and continue to overwrite the invalid cache item
$this->logger->error($e->getMessage());
}
$name = self::buildName($postId);
// start tracing of nested cache tags and system config keys
$response = $this->tracer->trace($name, function () use ($criteria, $context, $postId) {
return $this->getDecorated()->load($postId, $criteria, $context);
});
// compress cache content to reduce cache size
$item = CacheCompressor::compress($item, $response);
$item->tag(array_merge(
// get traced tags and configs
$this->tracer->get(self::buildName($postId)),
[self::buildName($postId)]
));
$this->cache->save($item);
return $response;
}
public static function buildName(string $postId): string
{
return 'blog-post-route-' . $postId;
}
private function generateKey(string $postId, SalesChannelContext $context, Criteria $criteria): string
{
$parts = [
self::buildName($postId),
$this->generator->getCriteriaHash($criteria),
$this->generator->getSalesChannelContextHash($context),
];
return md5(JsonFieldSerializer::encodeJson($parts));
}
}