Skip to content

Commit

Permalink
Merge pull request Smile-SA#3447 from rbayet/feat-thesaurus-limit-rew…
Browse files Browse the repository at this point in the history
…rites-caching

[Thesaurus] Additional PHPUnit tests + Conditional caching
  • Loading branch information
rbayet authored Nov 27, 2024
2 parents 6d5616e + ac2289a commit 3018a00
Show file tree
Hide file tree
Showing 9 changed files with 2,239 additions and 6 deletions.
78 changes: 78 additions & 0 deletions src/module-elasticsuite-thesaurus/Config/ThesaurusCacheConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteThesaurus
* @author Richard BAYET <richard.bayet@smile.fr>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteThesaurus\Config;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;

/**
* Thesaurus cache configuration helper.
*
* @category Smile
* @package Smile\ElasticsuiteThesaurus
* @author Richard Bayet <richard.bayet@smile.fr>
*/
class ThesaurusCacheConfig
{
/** @var string */
const ALWAYS_CACHE_RESULTS_XML_PATH = 'smile_elasticsuite_thesaurus_settings/cache/always';

/** @var string */
const MIN_REWRITES_FOR_CACHING_XML_PATH = 'smile_elasticsuite_thesaurus_settings/cache/min_rewites';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Constructor.
*
* @param ScopeConfigInterface $scopeConfig Scope config interface.
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* Returns true if it is allowed by the config to store in cache the results of the thesaurus rules computation.
*
* @param ContainerConfigurationInterface $config Container configuration.
* @param int $rewritesCount Number of rewrites/alternative queries.
*
* @return bool
*/
public function isCacheStorageAllowed(ContainerConfigurationInterface $config, $rewritesCount)
{
$alwaysCache = $this->scopeConfig->isSetFlag(
self::ALWAYS_CACHE_RESULTS_XML_PATH,
ScopeInterface::SCOPE_STORES,
$config->getStoreId()
);

if (false === $alwaysCache) {
$minRewritesForCaching = $this->scopeConfig->getValue(
self::MIN_REWRITES_FOR_CACHING_XML_PATH,
ScopeInterface::SCOPE_STORES,
$config->getStoreId()
);

return ($rewritesCount >= $minRewritesForCaching);
}

return true;
}
}
15 changes: 13 additions & 2 deletions src/module-elasticsuite-thesaurus/Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusConfigFactory;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusConfig;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusCacheConfig;
use Smile\ElasticsuiteThesaurus\Api\Data\ThesaurusInterface;
use Smile\ElasticsuiteCore\Helper\Cache as CacheHelper;

Expand Down Expand Up @@ -66,24 +67,32 @@ class Index
*/
private $cacheHelper;

/**
* @var ThesaurusConfig
*/
private $thesaurusCacheConfig;

/**
* Constructor.
*
* @param ClientInterface $client ES client.
* @param IndexSettingsHelper $indexSettingsHelper Index Settings Helper.
* @param CacheHelper $cacheHelper ES caching helper.
* @param ThesaurusConfigFactory $thesaurusConfigFactory Thesaurus configuration factory.
* @param ThesaurusCacheConfig $thesaurusCacheConfig Thesaurus cache configuration helper.
*/
public function __construct(
ClientInterface $client,
IndexSettingsHelper $indexSettingsHelper,
CacheHelper $cacheHelper,
ThesaurusConfigFactory $thesaurusConfigFactory
ThesaurusConfigFactory $thesaurusConfigFactory,
ThesaurusCacheConfig $thesaurusCacheConfig
) {
$this->client = $client;
$this->indexSettingsHelper = $indexSettingsHelper;
$this->thesaurusConfigFactory = $thesaurusConfigFactory;
$this->cacheHelper = $cacheHelper;
$this->thesaurusCacheConfig = $thesaurusCacheConfig;
}

/**
Expand All @@ -104,7 +113,9 @@ public function getQueryRewrites(ContainerConfigurationInterface $containerConfi

if ($queryRewrites === false) {
$queryRewrites = $this->computeQueryRewrites($containerConfig, $queryText, $originalBoost);
$this->cacheHelper->saveCache($cacheKey, $queryRewrites, $cacheTags);
if ($this->thesaurusCacheConfig->isCacheStorageAllowed($containerConfig, count($queryRewrites))) {
$this->cacheHelper->saveCache($cacheKey, $queryRewrites, $cacheTags);
}
}

return $queryRewrites;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteThesaurus
* @author Richard BAYET <richard.bayet@smile.fr>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/

declare(strict_types = 1);

namespace Smile\ElasticsuiteThesaurus\Test\Unit\Config;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusCacheConfig;

/**
* Thesaurus Cache Config helper unit tests.
*
* @category Smile
* @package Smile\ElasticsuiteThesaurus
* @author Richard BAYET <richard.bayet@smile.fr>
*/
class ThesaurusCacheConfigTest extends \PHPUnit\Framework\TestCase
{
/**
* Test the cache storage limitation behavior.
* @dataProvider cacheStorageLimitationDataProvider
*
* @param array $isSetFlagReturnsMap Map of return results for method 'isSetFlag'.
* @param array $getValueReturnsMap Map of return results for method 'getValue'.
* @param int $storeId Store Id.
* @param int $rewritesCount Number of rewrites/alternative queries.
* @param bool $expectedCacheStorageAllowed Expected cache storage allowed result.
*/
public function testCacheStorageLimitation(
$isSetFlagReturnsMap,
$getValueReturnsMap,
$storeId,
$rewritesCount,
$expectedCacheStorageAllowed
) {
$containerConfigMock = $this->getContainerConfigurationInterfaceMock();
$containerConfigMock->method('getStoreId')->willReturn($storeId);

$scopeConfigMock = $this->getScopeConfigInterfaceMock();
$scopeConfigMock->method('isSetFlag')->willReturnMap($isSetFlagReturnsMap);
$scopeConfigMock->method('getValue')->willReturnMap($getValueReturnsMap);

$thesaurusCacheConfig = new ThesaurusCacheConfig($scopeConfigMock);
$this->assertEquals(
$expectedCacheStorageAllowed,
$thesaurusCacheConfig->isCacheStorageAllowed($containerConfigMock, $rewritesCount)
);
}

/**
* Data provider for testCacheStorageLimitation.
*
* @return array
*/
public function cacheStorageLimitationDataProvider()
{
$isSetFlagReturnsMap = [
[ThesaurusCacheConfig::ALWAYS_CACHE_RESULTS_XML_PATH, ScopeInterface::SCOPE_STORES, 1, true],
[ThesaurusCacheConfig::ALWAYS_CACHE_RESULTS_XML_PATH, ScopeInterface::SCOPE_STORES, 2, false],
[ThesaurusCacheConfig::ALWAYS_CACHE_RESULTS_XML_PATH, ScopeInterface::SCOPE_STORES, 3, false],
];
$getValueReturnsMap = [
[ThesaurusCacheConfig::MIN_REWRITES_FOR_CACHING_XML_PATH, ScopeInterface::SCOPE_STORES, 1, 10],
[ThesaurusCacheConfig::MIN_REWRITES_FOR_CACHING_XML_PATH, ScopeInterface::SCOPE_STORES, 2, 0],
[ThesaurusCacheConfig::MIN_REWRITES_FOR_CACHING_XML_PATH, ScopeInterface::SCOPE_STORES, 3, 10],
];

return [
/*
* [isSetFlagReturnsMap, getValueReturnsMap, storeId, rewritesCount, expectedCacheStorageAllowed]
*/
// StoreId 1.
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 0, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 9, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 10, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 1, 11, true],
// StoreId 2.
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 0, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 9, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 10, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 2, 11, true],
// StoreId 3.
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 0, false],
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 9, false],
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 10, true],
[$isSetFlagReturnsMap, $getValueReturnsMap, 3, 11, true],
];
}

/**
* Get Container configuration mock.
*
* @return MockObject|ContainerConfigurationInterface
*/
private function getContainerConfigurationInterfaceMock()
{
$containerConfiguration = $this->getMockBuilder(ContainerConfigurationInterface::class)
->disableOriginalConstructor()
->getMock();

return $containerConfiguration;
}

/**
* Get Scope config mock.
*
* @return MockObject|ScopeConfigInterface
*/
private function getScopeConfigInterfaceMock()
{
$scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()
->getMock();

return $scopeConfig;
}
}
Loading

0 comments on commit 3018a00

Please sign in to comment.