Skip to content

Commit

Permalink
Fix #3234 Prevent to cache null query
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Apr 4, 2024
1 parent 75bbdc1 commit f3bca9b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/module-elasticsuite-virtual-category/Model/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function getSortedProductIds() : array
*
* @return QueryInterface
*/
private function getQueryFilter() : QueryInterface
private function getQueryFilter(): ?QueryInterface
{
$query = null;

Expand Down Expand Up @@ -148,7 +148,7 @@ private function getQueryFilter() : QueryInterface
*
* @return QueryInterface
*/
private function getEntityIdFilterQuery($ids) : QueryInterface
private function getEntityIdFilterQuery($ids): QueryInterface
{
return $this->queryFactory->create(QueryInterface::TYPE_TERMS, ['field' => 'entity_id', 'values' => $ids]);
}
Expand Down
31 changes: 27 additions & 4 deletions src/module-elasticsuite-virtual-category/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function __toString(): string
public function getCategorySearchQuery($category, $excludedCategories = []): ?QueryInterface
{
\Magento\Framework\Profiler::start('ES:Virtual Rule ' . __FUNCTION__);
$categoryId = !is_object($category) ? $category : $category->getId();
$categoryId = (int) !is_object($category) ? $category : $category->getId();
$cacheKey = implode(
'|',
[
Expand All @@ -188,7 +188,7 @@ public function getCategorySearchQuery($category, $excludedCategories = []): ?Qu
]
);

$query = self::$localCache[$categoryId] ?? false;
$query = $this->getFromLocalCache($categoryId);

// If the category is not an object, it can't be in a "draft" mode.
if ($query === false && (!is_object($category) || !$category->getHasDraftVirtualRule())) {
Expand All @@ -204,13 +204,13 @@ public function getCategorySearchQuery($category, $excludedCategories = []): ?Qu
}
$query = $this->buildCategorySearchQuery($category, $excludedCategories);

if (!$category->getHasDraftVirtualRule()) {
if (!$category->getHasDraftVirtualRule() && $query !== null) {
$cacheData = serialize($query);
$this->sharedCache->save($cacheData, $cacheKey, $category->getCacheTags());
}
}

self::$localCache[$categoryId] = $query;
$this->saveInLocalCache($categoryId, $query);
\Magento\Framework\Profiler::stop('ES:Virtual Rule ' . __FUNCTION__);

return $query;
Expand Down Expand Up @@ -520,4 +520,27 @@ private function getTreeRootCategoryId($category): int

return $rootCategoryId;
}

/**
* Get category query from local cache.
*
* @return QueryInterface|bool|null
*/
private function getFromLocalCache(int $categoryId): mixed
{
return self::$localCache[$categoryId] ?? false;
}

/**
* Save category query in local cache.
*
* @param int $categoryId In of the category.
* @param QueryInterface|bool|null $query Query of the category.
*/
private function saveInLocalCache(int $categoryId, mixed $query): void
{
if ($query !== null) {
self::$localCache[$categoryId] = $query;
}
}
}

0 comments on commit f3bca9b

Please sign in to comment.