Automatically Refresh Cache Using Cron in Magento 2

Posted on 25 September, 2017

In this tutorial, we will give you simple steps to create Extension for Automatically Refresh Cache using Cron in Magento 2 . Please follow below simple steps to create Auto Refresh Cache Extension In Magento 2.

Sometimes it may happen that we change the content/data of cms page,  block or other configuration at that time some of the caches has been invalidated so website performance can be down. Now just refresh invalidate cache automatically by setting cron. We will create one extension for to refresh invalidate cache automatically.

Follow some simple steps to create Auto refresh invalidate cache extension for Magento 2.

Namespace: Emipro Modulename: AutoRefreshCache

Step 1: Create registration.php into app/code/Emipro/AutoRefreshCache

1
2
3
<?php
\Magento\Framework\Component\ComponentRegistrar::register
(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Emipro_AutoRefreshCache',__DIR__);

Step 2: Create module.xml into app/code/Emipro/AutoRefreshCache/etc

1
2
3
4
5
<?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="Emipro_AutoRefreshCache" setup_version="1.0.0" schema_version="0.0.1">
    </module>
</config>

Step 3: Create crontab.xml into app/code/Emipro/AutoRefreshCache/etc

1
2
3
4
5
6
7
<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="emipro_autorefreshcache_cron" instance="Emipro\AutoRefreshCache\Model\Cron" method="myCronFunction">
            <schedule>*/2 * * * *</schedule>
        </job>
    </group>
</config>

Setp 4: Create Cron.php into app/code/Emipro/AutoRefreshCache/Model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace Emipro\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/magentosetup:upgrade --keep-generated

php bin/magento cache:flush

For more informations about Magento 2 Useful Commands List Click Here and read our another technical note.

Step 6: To set cron on your server you need to run following commands.

crontab -e

And then type the follwoingcroncofiguration for your magento2 , you can set cron schedule as per your requirement.

*/2 * * * * php /var/www/html/magento2/bin/magentocron: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.

I hope this Magento technical note 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 technical note.

P.S. Do share this note with your team.


Rakesh Savaliya , eCommerce Engineer

Magento Technical Notes

About Emipro

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. Our customers are companies of all sizes ranging from startups to large enterprises who realize that they need a professional internet solution to generate revenue streams, establish proper communication channels, to achieve desired goals and streamline business operations. [....] Read More

Our writings seems informative ?

Subscribe for our Magento Technical Notes and get more amazing stuff directly to your inbox!

Post Your Review

X

Your Review has been posted

1 Comment(s)

Cesar

Posted on 25 November, 2017

Amazing! Thank you for your time to share this valuable code ;)