Refresh magento cache programmatically
Refresh magento cache programmatically

Now a days every frame-work using caching system for making website faster ans faster. Among these race magento is one of them and also uses excellent caching system in its structure. Magento has these ‘apc’, ‘memcached’, ‘xcache’,’zendserver_shmem’, ‘zendserver_disk’, ‘varien_eaccelerator’ caching engine to cache all files into cache files.So from these sysetm if we want manage to refresh then we can do easily by just accessing cache model and using that methods.So amongg from those some of possibilities are explained below.So lets take a look how we Refresh magento cache programmatically.
If you call Mage::app()->getCacheInstance()->getTypes() then the possible types will be as follows:

  • config
  • layout
  • block_html
  • translate
  • collections
  • eav
  • config_api
  • config_api2

So if you want to flush individually and want to skip some cache type then you can use below code :

$type = 'block_html';//an type you want fluch here
Mage::app()->getCacheInstance()->cleanType($type);

or

try {
	$allTypes = Mage::app()->useCache();
	foreach($allTypes as $type => $cache) {
		Mage::app()->getCacheInstance()->cleanType($type);
	}
} catch (Exception $e) {
	Mage::logException($e->getMessage());
}

But if you want to flush total cache of magento store then you can use the below code:

Mage::app()->cleanCache();
or
Mage::app()->getCacheInstance()->flush();

but in some cases you want to do it through custom script then you can use below code as I have prepared below code a stand alone script.You can place that php file in root directory of magento and run that in browser followed by base url like www.domain.com/customflush.php

< ?php
echo "Start Cleaning all caches ... \n\n";
ini_set("display_errors", 1);

require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);
Mage::getConfig()->init();
$types = Mage::app()->getCacheInstance()->getTypes();
 
try {
    echo "Cleaning data cache... \n";
    flush();
    foreach ($types as $type => $data) {
        echo "Removing $type ... ";
        echo Mage::app()->getCacheInstance()->clean($data["tags"]) ? "[OK]" : "[ERROR]";
        echo "\n";
    }
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}
 
echo "\n";
 
try {
    echo "Cleaning stored cache... ";
    flush();
    echo Mage::app()->getCacheInstance()->clean() ? "[OK]" : "[ERROR]";
    echo "\n\n";
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}
 
try {
    echo "Cleaning merged JS/CSS...";
    flush();
    Mage::getModel('core/design_package')->cleanMergedJsCss();
    Mage::dispatchEvent('clean_media_cache_after');
    echo "[OK]\n\n";
} catch (Exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}
 
try {
    echo "Cleaning image cache... ";
    flush();
    echo Mage::getModel('catalog/product_image')->clearCache();
    echo "[OK]\n";
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}
?>
Refresh magento cache programmatically
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *