Skip to content

Commit

Permalink
Merge pull request #1063 from romainruaud/fix_cache-policy
Browse files Browse the repository at this point in the history
Fix cache policy
  • Loading branch information
romainruaud authored Nov 21, 2018
2 parents 2ed106b + 8d4a1e2 commit c0343b4
Show file tree
Hide file tree
Showing 11 changed files with 655 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Smile\ElasticsuiteCatalogOptimizer\Controller\Adminhtml\Optimizer;

use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface;
use Smile\ElasticsuiteCatalogOptimizer\Controller\Adminhtml\AbstractOptimizer as OptimizerController;

/**
Expand All @@ -36,7 +37,7 @@ public function execute()
$redirectBack = $this->getRequest()->getParam('back', false);

if ($data) {
$identifier = $this->getRequest()->getParam('id');
$identifier = $this->getRequest()->getParam(OptimizerInterface::OPTIMIZER_ID);
$model = $this->optimizerFactory->create();

if ($identifier) {
Expand Down
45 changes: 33 additions & 12 deletions src/module-elasticsuite-catalog-optimizer/Model/Optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
namespace Smile\ElasticsuiteCatalogOptimizer\Model;

use Magento\Framework\DataObject\IdentityInterface;
use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface;

/**
Expand All @@ -23,7 +24,7 @@
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Fanny DECLERCK <fadec@smile.fr>
*/
class Optimizer extends \Magento\Framework\Model\AbstractModel implements OptimizerInterface
class Optimizer extends \Magento\Framework\Model\AbstractModel implements OptimizerInterface, IdentityInterface
{
/**
* @var string
Expand All @@ -45,6 +46,12 @@ class Optimizer extends \Magento\Framework\Model\AbstractModel implements Optimi
*/
private $serializer;

/**
* @var \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Limitation\IdentitiesFactory
*/
private $limitationIdentitiesFactory;

/**
/**
* @var string
*/
Expand All @@ -53,29 +60,32 @@ class Optimizer extends \Magento\Framework\Model\AbstractModel implements Optimi
/**
* Class constructor
*
* @param \Magento\Framework\Model\Context $context Context.
* @param \Magento\Framework\Registry $registry Registry.
* @param \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory Rule factory.
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter Date Filter.
* @param \Magento\Framework\Serialize\SerializerInterface $serializer Serializer.
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource Resource.
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection Resource collection.
* @param array $data Data.
* @param \Magento\Framework\Model\Context $context Context.
* @param \Magento\Framework\Registry $registry Registry.
* @param \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory Rule factory.
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter Date Filter.
* @param \Magento\Framework\Serialize\SerializerInterface $serializer Serializer.
* @param Optimizer\Limitation\IdentitiesFactory $limitationIdentitiesFactory Limitation Identities.
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource Resource.
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection Resource collection.
* @param array $data Data.
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
\Magento\Framework\Serialize\SerializerInterface $serializer,
Optimizer\Limitation\IdentitiesFactory $limitationIdentitiesFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->ruleFactory = $ruleFactory;
$this->dateFilter = $dateFilter;
$this->serializer = $serializer;
$this->ruleFactory = $ruleFactory;
$this->dateFilter = $dateFilter;
$this->serializer = $serializer;
$this->limitationIdentitiesFactory = $limitationIdentitiesFactory;
}

/**
Expand Down Expand Up @@ -304,6 +314,17 @@ public function validateData(\Magento\Framework\DataObject $dataObject)
return !empty($result) ? $result : true;
}

/**
* {@inheritdoc}
*/
public function getIdentities()
{
$limitationIdentities = $this->limitationIdentitiesFactory->create(['optimizer' => $this]);
$identities = array_merge($this->getCacheTags(), $limitationIdentities->get());

return $identities;
}

/**
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*
Expand Down
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
);
}
}
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;
}
}
Loading

0 comments on commit c0343b4

Please sign in to comment.