-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1063 from romainruaud/fix_cache-policy
Fix cache policy
- Loading branch information
Showing
11 changed files
with
655 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
src/module-elasticsuite-catalog-optimizer/Model/Optimizer/Limitation/Identities.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalogOptimizer | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Limitation; | ||
|
||
use Magento\Search\Model\PopularSearchTerms; | ||
use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface; | ||
|
||
/** | ||
* Identities Provider for optimizer limitations. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalogOptimizer | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class Identities | ||
{ | ||
/** | ||
* Scope configuration | ||
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* Catalog search data | ||
* | ||
* @var \Magento\Search\Model\ResourceModel\Query\Collection | ||
*/ | ||
private $queryCollection; | ||
|
||
/** | ||
* @var OptimizerInterface | ||
*/ | ||
private $optimizer; | ||
|
||
/** | ||
* Limitation Identities Constructor. | ||
* | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig Scope Config | ||
* @param \Magento\Search\Model\ResourceModel\Query\Collection $queryCollection Search Queries Collection | ||
* @param \Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface $optimizer The Optimizer | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | ||
\Magento\Search\Model\ResourceModel\Query\Collection $queryCollection, | ||
OptimizerInterface $optimizer | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
$this->queryCollection = $queryCollection; | ||
$this->optimizer = $optimizer; | ||
} | ||
|
||
/** | ||
* Get Limitation identities for the current optimizer. | ||
* | ||
* @return array | ||
*/ | ||
public function get() | ||
{ | ||
$identities = []; | ||
$origData = $this->optimizer->getOrigData(); | ||
$containers = $this->optimizer->getData('search_container') ?? []; | ||
|
||
if (!$this->optimizer->isObjectNew()) { | ||
$containers = array_unique( | ||
array_keys(array_merge($this->optimizer->getSearchContainers(), $origData['search_containers'] ?? [])) | ||
); | ||
} | ||
|
||
if (in_array('quick_search_container', $containers)) { | ||
$identities = array_merge($identities, $this->getSearchQueryIdentities()); | ||
} | ||
|
||
if (in_array('catalog_view_container', $containers)) { | ||
$identities = array_merge($identities, $this->getCategoryIdentities()); | ||
} | ||
|
||
return $identities; | ||
} | ||
|
||
/** | ||
* Get search queries identities related to current optimizer. | ||
* | ||
* @return array | ||
*/ | ||
private function getSearchQueryIdentities() | ||
{ | ||
$identities = []; | ||
$queryIds = []; | ||
$origData = $this->optimizer->getOrigData(); | ||
$data = $this->optimizer->getData(); | ||
|
||
// If optimizer was previously assigned to all queries, or is now set to all queries. | ||
$isAppliedToAllQueries = empty($data['quick_search_container']) | ||
|| (bool) $data['quick_search_container']['apply_to'] === false; | ||
$wasAppliedToAllQueries = empty($origData['quick_search_container']['query_ids']); | ||
|
||
if (!empty($origData['quick_search_container']['query_ids'])) { | ||
$queryIds = array_merge($queryIds, $origData['quick_search_container']['query_ids']); | ||
} | ||
|
||
if (!empty($data['quick_search_container']['query_ids'])) { | ||
foreach ($data['quick_search_container']['query_ids'] as $query) { | ||
$queryIds[] = $query['id']; | ||
} | ||
} | ||
|
||
$queryIds = array_unique(array_filter($queryIds)); | ||
|
||
if ($wasAppliedToAllQueries || $isAppliedToAllQueries) { | ||
$identities[] = \Smile\ElasticsuiteCatalog\Block\CatalogSearch\Result\Cache::POPULAR_SEARCH_CACHE_TAG; | ||
} elseif (!empty($queryIds)) { | ||
$popularQueryIds = $this->queryCollection | ||
->setPopularQueryFilter($this->optimizer->getStoreId()) | ||
->setPageSize($this->getMaxCountCacheableSearchTerms($this->optimizer->getStoreId())) | ||
->load() | ||
->getColumnValues('query_id'); | ||
|
||
if (!empty(array_intersect($queryIds, $popularQueryIds))) { | ||
$identities[] = \Smile\ElasticsuiteCatalog\Block\CatalogSearch\Result\Cache::POPULAR_SEARCH_CACHE_TAG; | ||
} | ||
} | ||
|
||
return $identities; | ||
} | ||
|
||
/** | ||
* Get category identities related to current optimizer. | ||
* | ||
* @return array | ||
*/ | ||
private function getCategoryIdentities() | ||
{ | ||
$identities = []; | ||
$categoryIds = []; | ||
$origData = $this->optimizer->getOrigData(); | ||
$data = $this->optimizer->getData(); | ||
|
||
// If optimizer was previously assigned to all categories, or is now set to all categories. | ||
$isAppliedToAllCategories = empty($data['catalog_view_container']) | ||
|| (bool) $data['catalog_view_container']['apply_to'] === false; | ||
|
||
$wasAppliedToAllCategories = empty($origData['catalog_view_container']['category_ids']); | ||
|
||
if ($isAppliedToAllCategories || $wasAppliedToAllCategories) { | ||
$identities[] = \Magento\Catalog\Model\Category::CACHE_TAG; | ||
} | ||
|
||
if (!empty($data['catalog_view_container']['category_ids'])) { | ||
$categoryIds = array_merge($categoryIds, $data['catalog_view_container']['category_ids']); | ||
} | ||
|
||
if (!empty($origData['catalog_view_container']['category_ids'])) { | ||
$categoryIds = array_merge($categoryIds, $origData['catalog_view_container']['category_ids']); | ||
} | ||
|
||
$categoryIds = array_filter(array_unique($categoryIds)); | ||
if (!empty($categoryIds)) { | ||
$categoryTags = array_map(function ($categoryId) { | ||
return \Magento\Catalog\Model\Category::CACHE_TAG . '_' . $categoryId; | ||
}, $categoryIds); | ||
|
||
$identities = array_merge($identities, $categoryTags); | ||
} | ||
|
||
return $identities; | ||
} | ||
|
||
/** | ||
* Retrieve maximum count cacheable search terms by Store. | ||
* | ||
* @param int $storeId Store Id | ||
* | ||
* @return int | ||
*/ | ||
private function getMaxCountCacheableSearchTerms(int $storeId) | ||
{ | ||
return $this->scopeConfig->getValue( | ||
PopularSearchTerms::XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/module-elasticsuite-catalog/Block/CatalogSearch/Result/Cache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Block\CatalogSearch\Result; | ||
|
||
use Magento\Framework\DataObject\IdentityInterface; | ||
use Magento\Framework\View\Element\AbstractBlock; | ||
|
||
/** | ||
* Block to handle search results cache tags. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class Cache extends AbstractBlock implements IdentityInterface | ||
{ | ||
/** | ||
* Cache tag that will be applied to popular search results (they are cached by Magento). | ||
*/ | ||
const POPULAR_SEARCH_CACHE_TAG = 'es_pop'; // Short name style like Magento does (cat_c, cat_p, etc...). | ||
|
||
/** | ||
* @var \Magento\Framework\App\ResponseInterface | ||
*/ | ||
private $response; | ||
|
||
/** | ||
* Cache constructor. | ||
* | ||
* @param \Magento\Framework\View\Element\Context $context Block Context | ||
* @param \Magento\Framework\App\ResponseInterface $response HTTP Response | ||
* @param array $data Data | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\View\Element\Context $context, | ||
\Magento\Framework\App\ResponseInterface $response, | ||
array $data = [] | ||
) { | ||
$this->response = $response; | ||
parent::__construct($context, $data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIdentities() | ||
{ | ||
$identities = []; | ||
|
||
if ($this->isPageCacheable()) { | ||
$identities[] = self::POPULAR_SEARCH_CACHE_TAG; | ||
} | ||
|
||
return $identities; | ||
} | ||
|
||
/** | ||
* Check if current page is cacheable | ||
* | ||
* @return bool | ||
*/ | ||
public function isPageCacheable() | ||
{ | ||
$result = false; | ||
$pragma = $this->response->getHeader('pragma'); | ||
|
||
if ($pragma) { | ||
$result = $pragma->getFieldValue() === 'cache'; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.