Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#29654 Object Cache for Category Links #39420

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\CategoryLinkInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;

/**
* Product CategoryLink resource model
*/
class CategoryLink
{
/**
* @var \Magento\Framework\EntityManager\MetadataPool
* @var MetadataPool
*/
private $metadataPool;

Expand All @@ -31,13 +34,16 @@ class CategoryLink
private $categoryLinkMetadata;

/**
* CategoryLink constructor.
*
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @var array
*/
private $productLinks = [];

/**
* @param MetadataPool $metadataPool
* @param ResourceConnection $resourceConnection
*/
public function __construct(
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
MetadataPool $metadataPool,
ResourceConnection $resourceConnection
) {
$this->metadataPool = $metadataPool;
Expand All @@ -50,22 +56,29 @@ public function __construct(
* @param ProductInterface $product
* @param array $categoryIds
* @return array
*
* @TODO This still can be improved with caching single categories and request only non-cached ones
*/
public function getCategoryLinks(ProductInterface $product, array $categoryIds = [])
{
$connection = $this->resourceConnection->getConnection();
$productId = (int)$product->getId();
$cacheKey = $this->getCategoryLinksCacheKey($categoryIds, $productId);

$select = $connection->select();
$select->from($this->getCategoryLinkMetadata()->getEntityTable(), ['category_id', 'position']);
$select->where('product_id = ?', (int)$product->getId());
if (!isset($this->productLinks[$cacheKey])) {
$connection = $this->resourceConnection->getConnection();

if (!empty($categoryIds)) {
$select->where('category_id IN(?)', $categoryIds);
}
$select = $connection->select();
$select->from($this->getCategoryLinkMetadata()->getEntityTable(), ['category_id', 'position']);
$select->where('product_id = ?', $productId);

$result = $connection->fetchAll($select);
if (!empty($categoryIds)) {
$select->where('category_id IN(?)', $categoryIds);
}

return $result;
$this->productLinks[$cacheKey] = $connection->fetchAll($select);
}

return $this->productLinks[$cacheKey];
}

/**
Expand Down Expand Up @@ -181,6 +194,8 @@ public function updateCategoryLinks(ProductInterface $product, array $insertLink
}
}

$this->cleanCategoryLinksCache();

return array_column($insertLinks, 'category_id');
}

Expand All @@ -203,6 +218,8 @@ private function deleteCategoryLinks(ProductInterface $product, array $deleteLin
'category_id IN(?)' => array_column($deleteLinks, 'category_id')
]);

$this->cleanCategoryLinksCache();

return array_column($deleteLinks, 'category_id');
}

Expand Down Expand Up @@ -252,4 +269,37 @@ private function analyseUpdatedLinks($deleteUpdate, $insertUpdate)

return [$delete, $insert, $insertUpdate['updated']];
}

/**
* Returns cache key based on product ID and category IDs
*
* Category IDs were filtered out and sorted to avoid fetching the same data from Database,
* when the list of categories is provided in different order.
*
* @param array $categoryIds
* @param int $productId
* @return string
*/
private function getCategoryLinksCacheKey(array $categoryIds, int $productId): string
{
$categoryIds = array_filter(array_map('intval', $categoryIds));
sort($categoryIds);

return sprintf('%d|%s', $productId, implode(',', $categoryIds));
}

/**
* Removes ObjectCache for Category Links
*
* @return void
*/
private function cleanCategoryLinksCache(?string $cacheKey = null): void
{
if ($cacheKey) {
unset($this->productLinks[$cacheKey]);
return;
}

$this->productLinks = [];
}
}