Skip to content

Commit c3eaac2

Browse files
committed
Reduce SQL queries for DB layout update check. Remove 'Plugin' from class name.
1 parent c7ae442 commit c3eaac2

File tree

3 files changed

+30
-66
lines changed

3 files changed

+30
-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,34 @@
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;
1112
use Magento\Store\Model\StoreManagerInterface;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\Framework\View\Layout\ProcessorInterface;
15+
use Magento\Widget\Model\ResourceModel\Layout\Update\CollectionFactory;
1416
use Vendic\OptimizeCacheSize\Model\Config;
15-
use Magento\Widget\Model\ResourceModel\Layout\Update;
1617
use Magento\Framework\Exception\NoSuchEntityException;
17-
use Vendic\OptimizeCacheSize\Model\LayoutUpdateFetcher;
1818

19-
class RemoveHandlersPlugin
19+
class RemoveHandlers
2020
{
21-
2221
private const string PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_';
2322
private const string PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_';
2423
private const string CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_';
2524

25+
private array $dbLayoutHandlers = [];
26+
2627
public function __construct(
2728
private readonly Config $config,
28-
private readonly LayoutUpdateFetcher $layoutUpdateFetcher,
2929
private readonly StoreManagerInterface $storeManager,
30-
private readonly DesignInterface $design
30+
private readonly DesignInterface $design,
31+
private readonly CollectionFactory $layoutUpdateCollectionFactory
3132
) {
3233
}
3334

@@ -44,17 +45,11 @@ public function afterAddHandle(
4445
return $result;
4546
}
4647

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) {
48+
foreach ($result->getHandles() as $handler) {
5449
if (
5550
$this->config->isRemoveCategoryIdHandlers()
5651
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)
57-
&& !in_array($handler, $dbLayoutHandlers)
52+
&& !$this->hasDbLayoutUpdate($handler)
5853
) {
5954
$result->removeHandle($handler);
6055
continue;
@@ -63,7 +58,7 @@ public function afterAddHandle(
6358
if (
6459
$this->config->isRemoveProductIdHandlers()
6560
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)
66-
&& !in_array($handler, $dbLayoutHandlers)
61+
&& !$this->hasDbLayoutUpdate($handler)
6762
) {
6863
$result->removeHandle($handler);
6964
continue;
@@ -72,11 +67,27 @@ public function afterAddHandle(
7267
if (
7368
$this->config->isRemoveProductSkuHandlers()
7469
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)
75-
&& !in_array($handler, $dbLayoutHandlers)
70+
&& !$this->hasDbLayoutUpdate($handler)
7671
) {
7772
$result->removeHandle($handler);
7873
}
7974
}
8075
return $result;
8176
}
77+
78+
private function hasDbLayoutUpdate(string $handler): bool
79+
{
80+
$storeId = (int)$this->storeManager->getStore()->getId();
81+
$themeId = (int)$this->design->getDesignTheme()->getId();
82+
83+
if (!isset($this->dbLayoutHandlers[$storeId][$themeId][$handler])) {
84+
$updateCollection = $this->layoutUpdateCollectionFactory->create();
85+
$updateCollection->addStoreFilter($storeId);
86+
$updateCollection->addThemeFilter($themeId);
87+
$updateCollection->addFieldToFilter('handle', $handler);
88+
$this->dbLayoutHandlers[$storeId][$themeId][$handler] = $updateCollection->getSize() > 0;
89+
}
90+
91+
return $this->dbLayoutHandlers[$storeId][$themeId][$handler];
92+
}
8293
}

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)