Automatically refresh cache using cron in Magento.

Posted on 11 June, 2016

Below script Is used to automatically refresh cache In Magento. This Script doesn’t refresh all the cache every time it will run, but it will refresh only an invalid cache which is required to refresh.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
ini_set('max_execution_time', 18000);
require_once 'app/Mage.php';
$app = Mage::app('admin');
umask(0);
Mage::setIsDeveloperMode(true);
//Cache Refresh Start
$invalidatedTypes = Mage::app()->getCacheInstance()->getInvalidatedTypes();
foreach ($invalidatedTypes as $type) {
    Mage::app()->getCacheInstance()->cleanType($type->getId());
    Mage::log('Cache Type '.$type->getId()." Is Refresh.",null,'Cache_AutoRefresh.log');
}
//Cache Refresh End
?>

Following code gives you an array of invalid status of cache which is required to refresh.

$invalidatedTypes = Mage::app()->getCacheInstance()->getInvalidatedTypes();

Using the following line you can refresh a particular cache type by using its Id for example "block_html"

Mage::app()->getCacheInstance()->cleanType('Cache_Type_Id_Here');

In the following code we have created a log file using following line.

Mage::log('Cache Type '.$type->getId()." Is Refresh.",null,'Cache_AutoRefresh.log');

So you can check the refreshed cache log on "Cache_AutoRefresh.log" file which is located in "var/log/Cache_AutoRefresh.log".

To automatically refresh cache you just need to create one file on Magento root folder. For Example: Autocroncache.php And Set Cron For "Autocroncache.php" File.

How To Set A Cron Job

1. First, open the Terminal.

2. Login with your SSH username & password.
Make sure you use your own domain name or IP address. If you want a user other than root, type that               username before the @ symbol.

Type:
    ssh root@example.com
    OR (IP address version)
    ssh root@70.32.86.175
    OR (domain FTP user version)
    ssh domainuser@example.com

3. Now type "crontab -e" after that command:

[root@current ~]#crontab -e

4. And Then Enter Following Command

Note: 30 12 indicates time 12:30, and * indicate that it should run every day of every month

/30 12 * * * sh /path/to/your/Autocroncache.php

5. Save and you are done.


Mayank Zalavadia , eCommerce Project Manager

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

0 Comment(s)