Hey Friends
In Today’s tutorial we are showing How you can clear your magento’s Full Page Cache using cron
First of all create a Module as below
app/code/NameSpace/FpcRefreshCache
create a file registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register (\Magento\Framework\Component\ComponentRegistrar::MODULE,'NameSpace_FpcRefreshCache',__DIR__);?>
Create a module.xml under app/code/NameSpace/FpcRefreshCache/etc folder
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="NameSpace_FpcRefreshCache" setup_version="1.0.0" schema_version="0.0.1"> </module> </config>
Create File NameSpace/FpcRefreshCache/Cron/ClearCache.php
<?php namespace NameSpace\AutoRefreshCache\Cron; use Magento\Framework\App\Cache\Manager; use Magento\Framework\Event\ManagerInterface; use Magento\PageCache\Model\Cache\Type as PageCacheType; use Psr\Log\LoggerInterface as Logger; class ClearCache { /** * @var ManagerInterface */ protected $eventManager; /** * @var Logger */ protected $logger; /** * @var Manager */ private $cacheManager; /** * ClearCache constructor. * @param ManagerInterface $eventManager * @param Logger $logger * @param Manager $cacheManager */ public function __construct( ManagerInterface $eventManager, Logger $logger, Manager $cacheManager ) { $this->eventManager = $eventManager; $this->logger = $logger; $this->cacheManager = $cacheManager; } /** * Clear the cache on a time interval to Fix the add to cart issue */ public function execute() { $this->eventManager->dispatch('adminhtml_cache_flush_all'); $this->cacheManager->clean([PageCacheType::TYPE_IDENTIFIER]); $this->logger->info('Cache Cleaned - '.PageCacheType::TYPE_IDENTIFIER); } }//end class
Create a file crontab.xml
<group id="default"> <job name="namespace_fpcrefreshcache_cron" instance="NameSpace\FpcRefreshCache\Cron\ClearCache" method="execute"> <schedule>0 */4 * * *</schedule> </job> </group>
Hope You like our this solution! Please vist again.