Skip to content

Commit

Permalink
[framework] InMemoryCache for easier managing of caches (#3031)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik authored Sep 23, 2024
2 parents 1b4f5da + ef40d20 commit 71da4c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/Model/FeedItem/HeurekaFeedItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,34 @@

namespace Shopsys\ProductFeed\HeurekaBundle\Model\FeedItem;

use Shopsys\FrameworkBundle\Component\Cache\InMemoryCache;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\Category\CategoryFacade;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade;
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPriceCalculationForCustomerUser;
use Shopsys\FrameworkBundle\Model\Product\Product;
use Shopsys\ProductFeed\HeurekaBundle\Model\HeurekaCategory\HeurekaCategoryFacade;
use Symfony\Contracts\Service\ResetInterface;

class HeurekaFeedItemFactory implements ResetInterface
class HeurekaFeedItemFactory
{
/**
* @var string[]|null[]
*/
protected array $heurekaCategoryFullNamesCache = [];
protected const string HEUREKA_CATEGORY_FULL_NAMES_CACHE_NAMESPACE = 'heurekaCategoryFullNames';

/**
* @param \Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPriceCalculationForCustomerUser $productPriceCalculationForCustomerUser
* @param \Shopsys\ProductFeed\HeurekaBundle\Model\FeedItem\HeurekaProductDataBatchLoader $productDataBatchLoader
* @param \Shopsys\ProductFeed\HeurekaBundle\Model\HeurekaCategory\HeurekaCategoryFacade $heurekaCategoryFacade
* @param \Shopsys\FrameworkBundle\Model\Category\CategoryFacade $categoryFacade
* @param \Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade $productAvailabilityFacade
* @param \Shopsys\FrameworkBundle\Component\Cache\InMemoryCache $inMemoryCache
*/
public function __construct(
protected readonly ProductPriceCalculationForCustomerUser $productPriceCalculationForCustomerUser,
protected readonly HeurekaProductDataBatchLoader $productDataBatchLoader,
protected readonly HeurekaCategoryFacade $heurekaCategoryFacade,
protected readonly CategoryFacade $categoryFacade,
protected readonly ProductAvailabilityFacade $productAvailabilityFacade,
protected readonly InMemoryCache $inMemoryCache,
) {
}

Expand Down Expand Up @@ -109,13 +108,15 @@ protected function getHeurekaCategoryFullName(Product $product, DomainConfig $do
*/
protected function findHeurekaCategoryFullNameByCategoryIdUsingCache(int $categoryId): ?string
{
if (!array_key_exists($categoryId, $this->heurekaCategoryFullNamesCache)) {
$this->heurekaCategoryFullNamesCache[$categoryId] = $this->findHeurekaCategoryFullNameByCategoryId(
$categoryId,
);
}
$key = (string)$categoryId;

return $this->heurekaCategoryFullNamesCache[$categoryId];
return $this->inMemoryCache->getOrSaveValue(
static::HEUREKA_CATEGORY_FULL_NAMES_CACHE_NAMESPACE,
fn () => $this->findHeurekaCategoryFullNameByCategoryId(
$categoryId,
),
$key,
);
}

/**
Expand All @@ -128,9 +129,4 @@ protected function findHeurekaCategoryFullNameByCategoryId(int $categoryId): ?st

return $heurekaCategory !== null ? $heurekaCategory->getFullName() : null;
}

public function reset(): void
{
$this->heurekaCategoryFullNamesCache = [];
}
}
2 changes: 2 additions & 0 deletions tests/Unit/HeurekaFeedItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shopsys\FrameworkBundle\Component\Cache\InMemoryCache;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Money\Money;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected function setUp(): void
$this->heurekaCategoryFacadeMock,
$this->categoryFacadeMock,
$productAvailabilityFacadeMock,
new InMemoryCache(),
);

$this->defaultDomain = $this->createDomainConfigMock(Domain::FIRST_DOMAIN_ID, 'https://example.cz', 'cs');
Expand Down

0 comments on commit 71da4c5

Please sign in to comment.