SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH
use-auto-flush-cache-on-Magento
use auto flush cache on Magento

How to use auto flush cache on Magento 2?

Description

The flush cache is a very special function of Magento 2. The cache is a particular area of your hosting server. It helps to increase the page load speed by storing the web pages through browsers. Also, it reduces resource requirements in the situation of heavy traffic.

Every time we edit any information or configuration content after saving we have to clear the cache in admin or run it by command via terminal.

Today, we are going to do an extension auto flush cache, so that after each saves is automatically clear the cache no longer needs to run commands on the terminal or in the admin.

The files need to be added.

Create an module, observer and events.xml.

  • Create Magepow\AutoFlushCache\registration.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Magepow_AutoFlushCache',
__DIR__
);
  • Create Magepow\AutoFlushCache\module.xml
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magepow_AutoFlushCache">
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Config"/>
</sequence>
</module>
</config>
  • Create Magepow\AutoFlushCache\Observer\FlushCache.php
<?php
namespace Magepow\AutoFlushCache\Observer;

class FlushCache implements \Magento\Framework\Event\ObserverInterface
{
public function __construct(
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
$this->cacheTypeList = $cacheTypeList;
$this->cacheFrontendPool = $cacheFrontendPool;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
$this->cacheTypeList->cleanType($type);
}
foreach ($this->cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
}
} 

Final create file events.xml in etc.

Magepow\AutoFlushCache\etc\events.xml.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_postdispatch_adminhtml_system_config_save">
<observer name="auto_flush_cache" instance="Magepow\AutoFlushCache\Observer\FlushCache"/>
</event>
</config> 

Run the commands to install the module is we are using auto flush cache.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

Result

Hope this article will help you in some way, You can see useful articles in the next articles.

Anything you need support from Magento 2 feels free to contact us at Alothemes and

Phone: (+84)865633728

Email: support@alothemes.com