Skip to content

Commit 20ce788

Browse files
Initial commit
0 parents  commit 20ce788

File tree

10 files changed

+258
-0
lines changed

10 files changed

+258
-0
lines changed

Model/Config.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) Vendic B.V https://vendic.nl/
5+
*/
6+
7+
namespace Vendic\OptimizeCacheSize\Model;
8+
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Store\Model\ScopeInterface;
11+
12+
class Config
13+
{
14+
private const OCS_GENERAL_IS_ENABLED_PATH = 'optimize_cache_size/general/enabled';
15+
private const OCS_GENERAL_PRODUCT_ID_PATH = 'optimize_cache_size/general/product_id';
16+
private const OCS_GENERAL_PRODUCT_SKU_PATH = 'optimize_cache_size/general/product_sku';
17+
private const OCS_GENERAL_CATEGORY_ID_PATH = 'optimize_cache_size/general/category_id';
18+
19+
public function __construct(
20+
private ScopeConfigInterface $scopeConfig
21+
) {
22+
}
23+
24+
public function isModuleEnabled(int $store = 0): bool
25+
{
26+
return $this->scopeConfig->isSetFlag(
27+
self::OCS_GENERAL_IS_ENABLED_PATH,
28+
ScopeInterface::SCOPE_STORE,
29+
$store
30+
);
31+
}
32+
33+
public function isRemoveProductIdHandlers(int $store = 0): bool
34+
{
35+
return $this->scopeConfig->isSetFlag(
36+
self::OCS_GENERAL_PRODUCT_ID_PATH,
37+
ScopeInterface::SCOPE_STORE,
38+
$store
39+
);
40+
}
41+
42+
public function isRemoveProductSkuHandlers(int $store = 0): bool
43+
{
44+
return $this->scopeConfig->isSetFlag(
45+
self::OCS_GENERAL_PRODUCT_SKU_PATH,
46+
ScopeInterface::SCOPE_STORE,
47+
$store
48+
);
49+
}
50+
51+
public function isRemoveCategoryIdHandlers(int $store = 0): bool
52+
{
53+
return $this->scopeConfig->isSetFlag(
54+
self::OCS_GENERAL_CATEGORY_ID_PATH,
55+
ScopeInterface::SCOPE_STORE,
56+
$store
57+
);
58+
}
59+
}

Plugin/RemoveHandlersPlugin.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) Vendic B.V https://vendic.nl/
5+
*/
6+
7+
namespace Vendic\OptimizeCacheSize\Plugin;
8+
9+
use Magento\Framework\View\Layout\ProcessorInterface;
10+
use Vendic\OptimizeCacheSize\Model\Config;
11+
12+
class RemoveHandlersPlugin
13+
{
14+
15+
private const PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_';
16+
private const PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_';
17+
private const CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_';
18+
19+
public function __construct(
20+
private Config $config
21+
) {
22+
}
23+
24+
public function afterAddHandle(
25+
ProcessorInterface $subject,
26+
ProcessorInterface $result,
27+
array|string $handleName
28+
): ProcessorInterface {
29+
if (!$this->config->isModuleEnabled()) {
30+
return $result;
31+
}
32+
$handlers = $result->getHandles();
33+
foreach ($handlers as $handler) {
34+
if ($this->config->isRemoveCategoryIdHandlers()
35+
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)) {
36+
$result->removeHandle($handler);
37+
continue;
38+
}
39+
if ($this->config->isRemoveProductIdHandlers()
40+
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)) {
41+
$result->removeHandle($handler);
42+
continue;
43+
}
44+
if ($this->config->isRemoveProductSkuHandlers()
45+
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)) {
46+
$result->removeHandle($handler);
47+
}
48+
}
49+
return $result;
50+
}
51+
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Vendic_OptimizeCacheSize
2+
Magento 2 module that allows to remove handlers and reduces cache size
3+
4+
You can set up it here Stores -> Configuration -> Vendic -> Optimize Cache Size
5+
6+
## Installation
7+
```bash
8+
composer require vendic/magento2-optimize-cache-size
9+
```

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "vendic/module-optimize-cache-size",
3+
"version": "1.0.0",
4+
"description": "N/A",
5+
"type": "magento2-module",
6+
"require": {
7+
"magento/framework": "*",
8+
"magento/module-catalog": "104.0.*"
9+
},
10+
"license": [
11+
"Proprietary"
12+
],
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"Vendic\\OptimizeCacheSize\\": ""
19+
}
20+
}
21+
}

etc/acl.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright (c) Vendic B.V https://vendic.nl/
4+
-->
5+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
7+
<acl>
8+
<resources>
9+
<resource id="Magento_Backend::admin">
10+
<resource id="Vendic_OptimizeCacheSize::config" title="Optimize Cache Size Setup"/>
11+
</resource>
12+
</resources>
13+
</acl>
14+
</config>

etc/adminhtml/system.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright (c) Vendic B.V https://vendic.nl/
4+
-->
5+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
7+
<system>
8+
<tab id="vendic" translate="label" sortOrder="10">
9+
<label>Vendic</label>
10+
</tab>
11+
<section id="optimize_cache_size" translate="label" sortOrder="1000"
12+
showInDefault="1" showInWebsite="1" showInStore="1">
13+
<label>Optimize Cache Size</label>
14+
<tab>vendic</tab>
15+
<resource>Vendic_OptimizeCacheSize::config</resource>
16+
<group id="general" translate="label" sortOrder="10" showInStore="1" showInWebsite="1"
17+
showInDefault="1" type="text">
18+
<label>General</label>
19+
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1"
20+
showInWebsite="1" showInStore="1">
21+
<label>Enabled</label>
22+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
23+
</field>
24+
<field id="product_id" translate="label" type="select" sortOrder="20" showInDefault="1"
25+
showInWebsite="1" showInStore="1">
26+
<label>Remove Product ID Handlers</label>
27+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
28+
<comment>Removes catalog_product_view_id_ID handlers</comment>
29+
<depends>
30+
<field id="optimize_cache_size/general/enabled">1</field>
31+
</depends>
32+
</field>
33+
<field id="product_sku" translate="label" type="select" sortOrder="20" showInDefault="1"
34+
showInWebsite="1" showInStore="1">
35+
<label>Remove Product SKU Handlers</label>
36+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
37+
<comment>Removes catalog_product_view_sku_SKU handlers</comment>
38+
<depends>
39+
<field id="optimize_cache_size/general/enabled">1</field>
40+
</depends>
41+
</field>
42+
<field id="category_id" translate="label" type="select" sortOrder="30" showInDefault="1"
43+
showInWebsite="1" showInStore="1">
44+
<label>Remove Category ID Handlers</label>
45+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
46+
<comment>Removes catalog_category_view_id_ID handlers</comment>
47+
<depends>
48+
<field id="optimize_cache_size/general/enabled">1</field>
49+
</depends>
50+
</field>
51+
</group>
52+
</section>
53+
</system>
54+
</config>

etc/config.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright (c) Vendic B.V https://vendic.nl/
4+
-->
5+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
7+
<default>
8+
<optimize_cache_size>
9+
<general>
10+
<enabled>1</enabled>
11+
<product_id>1</product_id>
12+
<product_sku>1</product_sku>
13+
<category_id>1</category_id>
14+
</general>
15+
</optimize_cache_size>
16+
</default>
17+
</config>

etc/frontend/di.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright (c) Vendic B.V https://vendic.nl/
4+
-->
5+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
7+
<type name="Magento\Framework\View\Layout\ProcessorInterface">
8+
<plugin name="vendic_optimize_cache_size_remove_handlers"
9+
type="Vendic\OptimizeCacheSize\Plugin\RemoveHandlersPlugin"
10+
sortOrder="1"/>
11+
</type>
12+
</config>

etc/module.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright (c) Vendic B.V https://vendic.nl/
4+
-->
5+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
7+
<module name="Vendic_OptimizeCacheSize">
8+
<sequence>
9+
<module name="Magento_Catalog"/>
10+
</sequence>
11+
</module>
12+
</config>

registration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) Vendic B.V https://vendic.nl/
5+
*/
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendic_OptimizeCacheSize', __DIR__);

0 commit comments

Comments
 (0)