This article is a catch if you want to clean cache automatically after any admin configuration or product save.
Follow below two steps to get it done.
1. Create file event.xml on app/code/Magemonkeys/AutoFlushCache/etc
<?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="MagemonkeysAutoFlushCacheObserverFlushCache"/>
</event>
</config>
2. Create file FlushCache.php on app/code/Magemonkeys/AutoFlushCache/Observer
<?php
namespace MagemonkeysAutoFlushCacheObserver;
class FlushCache implements MagentoFrameworkEventObserverInterface
{
public function __construct(MagentoFrameworkAppCacheTypeListInterface $cacheTypeList, MagentoFrameworkAppCacheFrontendPool $cacheFrontendPool)
{
$this->cacheTypeList = $cacheTypeList;
$this->cacheFrontendPool = $cacheFrontendPool;
}
public function execute(MagentoFrameworkEventObserver $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();
}
}
}

