我们扩展了产品列表以包含特定的参数"属性组";从而影响输出。
在dev
模式下一切都很好,但当切换到prod
模式时,更改该参数不再有任何效果。
这似乎是缓存的,但为什么连URL参数都会更改呢?
即
/widgets/cms/navigation/67e6a05deae747e984a622c7e94e6881?no-aggregations=1&order=name-asc&p=1&property-groups=385b9ebbfc8e450bac9c4d63052ddf7f&slots=7b2547b253a145468fa35eb684b26a62
返回与相同的
/widgets/cms/navigation/67e6a05deae747e984a622c7e94e6881?no-aggregations=1&order=name-asc&p=1&property-groups=&slots=7b2547b253a145468fa35eb684b26a62
如果我们刷新完整的缓存,第二个调用会起作用并返回正确的结果。
我们当然不想完全禁用缓存,所以我们可能不得不以某种方式告诉Shopware6,在缓存密钥中使用新参数。
但是,我们找不到通过ShopwareStorefrontControllerCmsController::category
去bug进行缓存的位置。
问题似乎是:
ShopwareCoreContentProductSalesChannelListingCachedProductListingRoute::generateKey
private function generateKey(string $categoryId, Request $request, SalesChannelContext $context, Criteria $criteria): string
{
$parts = [
self::buildName($categoryId),
$this->generator->getCriteriaHash($criteria),
$this->generator->getSalesChannelContextHash($context),
];
$event = new ProductListingRouteCacheKeyEvent($parts, $categoryId, $request, $context, $criteria);
$this->dispatcher->dispatch($event);
return md5(JsonFieldSerializer::encodeJson($event->getParts()));
}
这里生成了一个缓存密钥,它考虑了标准参数,但没有考虑我们添加的新参数
您必须监听ProductListingRouteCacheKeyEvent
并将您的参数添加到部件中。
public static function getSubscribedEvents(): array
{
return [
// ...
ProductListingRouteCacheKeyEvent::class => 'addPropertyGroupsCachePart'
];
}
public function addPropertyGroupsCachePart(ProductListingRouteCacheKeyEvent $event)
{
$event->addPart((string)$event->getRequest()->get('poperty-groups'));
}
通过这种方式,Shopware生成一个唯一的缓存密钥,该密钥也基于我们引入的参数,缓存冲突消失。