Skip to content

Commit f7a39f6

Browse files
authored
Merge pull request #16 from Vendic/bugfix/VEOS-28
bugfix/VEOS-28: Reduce DB layout update queries.
2 parents c7ae442 + 143b68b commit f7a39f6

File tree

3 files changed

+31
-66
lines changed

3 files changed

+31
-66
lines changed

Model/LayoutUpdateFetcher.php

Lines changed: 0 additions & 47 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
<?php
2-
declare(strict_types=1);
2+
33
/**
44
* @copyright Copyright (c) Vendic B.V https://vendic.nl/
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Vendic\OptimizeCacheSize\Plugin;
810

9-
use Magento\Framework\App\ScopeInterface;
1011
use Magento\Framework\View\DesignInterface;
12+
use Magento\Store\Model\Store;
1113
use Magento\Store\Model\StoreManagerInterface;
1214
use Magento\Framework\Exception\LocalizedException;
1315
use Magento\Framework\View\Layout\ProcessorInterface;
16+
use Magento\Widget\Model\ResourceModel\Layout\Update\CollectionFactory;
1417
use Vendic\OptimizeCacheSize\Model\Config;
15-
use Magento\Widget\Model\ResourceModel\Layout\Update;
1618
use Magento\Framework\Exception\NoSuchEntityException;
17-
use Vendic\OptimizeCacheSize\Model\LayoutUpdateFetcher;
1819

19-
class RemoveHandlersPlugin
20+
class RemoveHandlers
2021
{
21-
2222
private const string PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_';
2323
private const string PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_';
2424
private const string CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_';
2525

26+
private array $dbLayoutHandlers = [];
27+
2628
public function __construct(
2729
private readonly Config $config,
28-
private readonly LayoutUpdateFetcher $layoutUpdateFetcher,
2930
private readonly StoreManagerInterface $storeManager,
30-
private readonly DesignInterface $design
31+
private readonly DesignInterface $design,
32+
private readonly CollectionFactory $layoutUpdateCollectionFactory
3133
) {
3234
}
3335

@@ -44,17 +46,11 @@ public function afterAddHandle(
4446
return $result;
4547
}
4648

47-
$store = (string)$this->storeManager->getStore()->getId();
48-
$theme = (string)$this->design->getDesignTheme()->getId();
49-
$handlers = $result->getHandles();
50-
51-
$dbLayoutHandlers = $this->layoutUpdateFetcher->fetchDbLayoutHandlers($handlers, $theme, $store);
52-
53-
foreach ($handlers as $handler) {
49+
foreach ($result->getHandles() as $handler) {
5450
if (
5551
$this->config->isRemoveCategoryIdHandlers()
5652
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)
57-
&& !in_array($handler, $dbLayoutHandlers)
53+
&& !$this->hasDbLayoutUpdate($handler)
5854
) {
5955
$result->removeHandle($handler);
6056
continue;
@@ -63,7 +59,7 @@ public function afterAddHandle(
6359
if (
6460
$this->config->isRemoveProductIdHandlers()
6561
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)
66-
&& !in_array($handler, $dbLayoutHandlers)
62+
&& !$this->hasDbLayoutUpdate($handler)
6763
) {
6864
$result->removeHandle($handler);
6965
continue;
@@ -72,11 +68,27 @@ public function afterAddHandle(
7268
if (
7369
$this->config->isRemoveProductSkuHandlers()
7470
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)
75-
&& !in_array($handler, $dbLayoutHandlers)
71+
&& !$this->hasDbLayoutUpdate($handler)
7672
) {
7773
$result->removeHandle($handler);
7874
}
7975
}
8076
return $result;
8177
}
78+
79+
private function hasDbLayoutUpdate(string $handler): bool
80+
{
81+
$storeId = (int)$this->storeManager->getStore()->getId();
82+
$themeId = (int)$this->design->getDesignTheme()->getId();
83+
84+
if (!isset($this->dbLayoutHandlers[$storeId][$themeId][$handler])) {
85+
$updateCollection = $this->layoutUpdateCollectionFactory->create();
86+
$updateCollection->addThemeFilter($themeId);
87+
$updateCollection->addFieldToFilter('store_id', [$storeId, Store::DEFAULT_STORE_ID]);
88+
$updateCollection->addFieldToFilter('handle', $handler);
89+
$this->dbLayoutHandlers[$storeId][$themeId][$handler] = $updateCollection->getSize() > 0;
90+
}
91+
92+
return $this->dbLayoutHandlers[$storeId][$themeId][$handler];
93+
}
8294
}

etc/frontend/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
77
<type name="Magento\Framework\View\Layout\ProcessorInterface">
88
<plugin name="vendic_optimize_cache_size_remove_handlers"
9-
type="Vendic\OptimizeCacheSize\Plugin\RemoveHandlersPlugin"
9+
type="Vendic\OptimizeCacheSize\Plugin\RemoveHandlers"
1010
sortOrder="1"/>
1111
</type>
1212
</config>

0 commit comments

Comments
 (0)