PHP Magento Tips

Refresh Magento Cache Programmatically

Rate this posting:
{[['']]}

Magento Caching system is designed for making website faster ans faster. Magento has these ‘apc’, ‘memcached’, ‘xcache’,’zendserver_shmem’, ‘zendserver_disk’, ‘varien_eaccelerator’ caching engine to cache all files into caching system. 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 among 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
You can look up the cache types in the core_cache_option database table:



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 installation 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() . "]");
}
?> 
Tag : Caching, Magento
0 Comments On "Refresh Magento Cache Programmatically"

Back To Top