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
28 changes: 27 additions & 1 deletion app/code/Magento/Catalog/Model/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use Magento\Framework\EntityManager\MetadataPool;
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\Store\Model\StoreManagerInterface;
use Magento\Framework\Phrase;

/**
* Repository for categories.
Expand Down Expand Up @@ -144,10 +146,12 @@ public function save(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 @@ -254,4 +258,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 @@ -168,6 +168,17 @@ public function testGetWhenCategoryDoesNotExist()
$this->assertEquals($categoryMock, $this->model->get($categoryId));
}

public function testGetWithStoreCodeException()
{
$categoryId = 5;
$categoryMock = $this->createMock(CategoryModel::class);
$this->expectException('\Magento\Framework\Exception\SerializationException');
$this->expectExceptionMessage(
'The "default" value\'s type is invalid. The "int" type was expected. Verify and try again.'
);
$this->assertEquals($categoryMock, $this->model->get($categoryId, 'default'));
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Cms\Api\GetBlockByIdentifierInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\SerializationException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\StoreManagementInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -143,6 +144,14 @@ public function testGetCategoryForProvidedStore(): void
$fixtureStoreId = $this->storeManager->getStore('fixturestore')->getId();
$categorySecondStore = $this->categoryRepository->get($categoryId, $fixtureStoreId);
$this->assertSame('category-fixturestore', $categorySecondStore->getUrlKey());

$caughtException = false;
try {
$this->categoryRepository->get($categoryId, 'default');
} catch (SerializationException $exception) {
$caughtException = true;
}
$this->assertTrue($caughtException);
}

/**
Expand Down