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

Added getScopeCacheKey in order to define the right cache key based on store ID #27142

Open
wants to merge 9 commits into
base: 2.5-develop
Choose a base branch
from
30 changes: 28 additions & 2 deletions app/code/Magento/Catalog/Model/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace Magento\Catalog\Model;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\SerializationException;
use Magento\Framework\Exception\StateException;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Phrase;

/**
* Repository for categories.
Expand Down Expand Up @@ -128,10 +130,12 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)

/**
* @inheritdoc
*
* @throws SerializationException
*/
public function get($categoryId, $storeId = null)
{
$cacheKey = $storeId ?? 'all';
$cacheKey = $this->getScopeCacheKey($storeId);
if (!isset($this->instances[$categoryId][$cacheKey])) {
/** @var Category $category */
$category = $this->categoryFactory->create();
Expand Down Expand Up @@ -238,4 +242,26 @@ private function getMetadataPool()
}
return $this->metadataPool;
}

/**
* Returns a cache key based on scope
*
* @param string|int|null $storeId
*
* @throws SerializationException
* @return int|string
*/
private function getScopeCacheKey($storeId = null)
{
if (null !== $storeId && !is_numeric($storeId)) {
throw new SerializationException(
new Phrase(
'The "%value" value\'s type is invalid. The "%type" type was expected. '
. 'Verify and try again.',
['value' => $storeId, 'type' => 'int']
)
);
}
return $storeId === null ? 'all' : (int)$storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreFactory;
use Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -46,6 +47,11 @@ class CategoryRepositoryTest extends TestCase
*/
private $categoryCollectionFactory;

/**
* @var StoreFactory
*/
private $storeFactory;

/**
* Sets up common objects.
*
Expand All @@ -57,6 +63,7 @@ protected function setUp()
$this->layoutManager = Bootstrap::getObjectManager()->get(CategoryLayoutUpdateManager::class);
$this->productCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
$this->categoryCollectionFactory = Bootstrap::getObjectManager()->create(CategoryCollectionFactory::class);
$this->storeFactory = Bootstrap::getObjectManager()->create(StoreFactory::class);
}

/**
Expand Down Expand Up @@ -151,14 +158,14 @@ public function testGetCategoryForProvidedStore()

$categoryFirstStore = $categoryRepository->get(
self::FIXTURE_TWO_STORES_CATEGORY_ID,
self::FIXTURE_FIRST_STORE_CODE
$this->storeFactory->create()->load(self::FIXTURE_FIRST_STORE_CODE)->getId()
);

$this->assertSame('category-defaultstore', $categoryFirstStore->getUrlKey());

$categorySecondStore = $categoryRepository->get(
self::FIXTURE_TWO_STORES_CATEGORY_ID,
self::FIXTURE_SECOND_STORE_CODE
$this->storeFactory->create()->load(self::FIXTURE_SECOND_STORE_CODE)->getId()
);

$this->assertSame('category-fixturestore', $categorySecondStore->getUrlKey());
Expand Down