Automatically Refresh Cache Using Cron in Magento 2
In this tutorial, we will give you simple steps to create Automatically refresh cache using cron in Magento 2 Extension. Please follow bellow simple steps to create auto refresh cache extension In Magento 2.
Some times it may happen that we change the content/data of cms page,block or other configuration at that time some of the cache has been invalidate so website performance can be down. Now just refresh cache automatically by setting cron. We will create one extension for to refresh cache automatically.
Follow some simple steps to create Auto refresh cache extension for Magento 2.
Namespace: Vendor Modulename: AutoRefreshCache
Step 1: Create registration.php into app/code/Vendor/AutoRefreshCache
saveCopyzoom_out_map<?php \Magento\Framework\Component\ComponentRegistrar::register (\Magento\Framework\Component\ComponentRegistrar::MODULE,'Vendor_AutoRefreshCache',__DIR__);
Step 2: Create module.xml into app/code/Vendor/AutoRefreshCache/etc
saveCopyzoom_out_map<?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="Vendor_AutoRefreshCache" setup_version="1.0.0" schema_version="0.0.1">
</module>
</config>
Step 3: Create crontab.xml into app/code/Vendor/AutoRefreshCache/etc
saveCopyzoom_out_map<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="vendor_autorefreshcache_cron" instance="Vendor\AutoRefreshCache\Model\Cron" method="myCronFunction">
<schedule>*/2 * * * *</schedule>
</job>
</group>
</config>
Setp 4: Create Cron.php into app/code/Vendor/AutoRefreshCache/Model
saveCopyzoom_out_map<?php
namespace Vendor\AutoRefreshCache\Model;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
use Magento\Framework\App\Cache\Manager as CacheManager;
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeListInterface;
class Cron
{
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
)
{
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
}
public function myCronFunction()
{
$invalidcache = $this->_cacheTypeList->getInvalidated();
foreach($invalidcache as $key => $value) {
$this->_cacheTypeList->cleanType($key);
}
}
}
Step 5: Open Command line in folder root of magento and run both commands.
php bin/magento setup:upgrade --keep-generated
php bin/magento cache:flush
For more informations about Magento 2 Useful Commands List Click Here and read our another Post.
Step 6: To set cron on your server you need to run following commands.
crontab -e
and then type the follwoing cron cofiguration for your magento 2 , you can set cron schedule as per your requirement.
*/2 * * * * php /var/www/html/magento2/bin/magento cron:run
Save this and exit
**Bingo! **
We are done Now you just do not worry about cache as it will automatically refresh. So by this way you can clean cache automatically in Magento2.
Automatically refresh cache using cron is not recommended way because in some cases maybe its cause issue in website performance if you constantly edit the content on your website. You can use this at your own risk.
I hope this Magento post helped you to create Automatically refresh cache extension in Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts on this post.
P.S. Do share this post with your team.
Review other articles maybe it'll help you too.