<?php declare(strict_types=1);
namespace Shopware\Core\Content\Product\SalesChannel\Search;
use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheKeyEvent;
use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheTagsEvent;
use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\StoreApiResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CachedProductSearchRoute extends AbstractProductSearchRoute
{
private const NAME = 'product-search-route';
private AbstractProductSearchRoute $decorated;
private CacheInterface $cache;
private EntityCacheKeyGenerator $generator;
/**
* @var AbstractCacheTracer<ProductSearchRouteResponse>
*/
private AbstractCacheTracer $tracer;
private array $states;
private EventDispatcherInterface $dispatcher;
/**
* @internal
*
* @param AbstractCacheTracer<ProductSearchRouteResponse> $tracer
*/
public function __construct(
AbstractProductSearchRoute $decorated,
CacheInterface $cache,
EntityCacheKeyGenerator $generator,
AbstractCacheTracer $tracer,
EventDispatcherInterface $dispatcher,
array $states
) {
$this->decorated = $decorated;
$this->cache = $cache;
$this->generator = $generator;
$this->tracer = $tracer;
$this->states = $states;
$this->dispatcher = $dispatcher;
}
public function getDecorated(): AbstractProductSearchRoute
{
return $this->decorated;
}
/**
* @Since("6.2.0.0")
* @Entity("product")
* @Route("/store-api/search", name="store-api.search", methods={"POST"})
*/
public function load(Request $request, SalesChannelContext $context, Criteria $criteria): ProductSearchRouteResponse
{
if ($context->hasState(...$this->states)) {
return $this->getDecorated()->load($request, $context, $criteria);
}
$key = $this->generateKey($request, $context, $criteria);
$value = $this->cache->get($key, function (ItemInterface $item) use ($request, $context, $criteria) {
$response = $this->tracer->trace(self::NAME, function () use ($request, $context, $criteria) {
return $this->getDecorated()->load($request, $context, $criteria);
});
$item->tag($this->generateTags($request, $response, $context, $criteria));
return CacheValueCompressor::compress($response);
});
return CacheValueCompressor::uncompress($value);
}
private function generateKey(Request $request, SalesChannelContext $context, Criteria $criteria): string
{
$parts = [
$this->generator->getCriteriaHash($criteria),
$this->generator->getSalesChannelContextHash($context),
$request->get('search'),
];
$event = new ProductSearchRouteCacheKeyEvent($parts, $request, $context, $criteria);
$this->dispatcher->dispatch($event);
return self::NAME . '-' . md5(JsonFieldSerializer::encodeJson($event->getParts()));
}
private function generateTags(Request $request, StoreApiResponse $response, SalesChannelContext $context, Criteria $criteria): array
{
$tags = array_merge(
$this->tracer->get(self::NAME),
[self::NAME]
);
$event = new ProductSearchRouteCacheTagsEvent($tags, $request, $response, $context, $criteria);
$this->dispatcher->dispatch($event);
return array_unique(array_filter($event->getTags()));
}
}