Saturday, June 2, 2012

Flush Magento Cache from Command Line or Standalone Script

There are several scripts floating around for this.  But unfortunately, if you have any kind of sophisticated cache (i.e. not using the Magento defaults) none of the scripts I could find will work.

The problem with them is they get a handle to the default Magento cache and attempt to flush it.  But if you are using Memcache or Redis, you’ve altered the “local.xml” and the default cache flush will not hit the proper cache.  When you do a manual cache flush via the control console, the “index.php” file calls Mage::run() which conditions the cache variables for you.  But since Mage::run() contains a dispatch() call, you cannot make a standalone page or a standalone script using this technique.

If you have reconfigured your cache, your local.xml has a section that looks something like this:


<config>
    <global>
        <cache>
            <backend>Zend_Cache_Backend_Redis</backend>
            <backend_options>
                <server>some_server_name.com</server>
                <port>some_port</port>
                <database>2</database>
                <force_standalone>0</force_standalone> 
                <automatic_cleaning_factor>20000</automatic_cleaning_factor>
            </backend_options>
        </cache>
        <full_page_cache>
            <backend>Zend_Cache_Backend_Redis</backend>
            <backend_options>
                <server>some_server_name.com</server>
                <port>some_port</port>
                <database>3</database>
                <force_standalone>0</force_standalone> 
                <automatic_cleaning_factor>20000</automatic_cleaning_factor>
            </backend_options>
        </full_page_cache>

In order to clean out that full page cache, you need  to read in this XML and use it as a parameter when you instantiate the cache model prior to attempting to a cache flush. 

The following script does this.  This script is meant to be called from “docroot/some_dir” via a standard “http” access as a standalone web page.  You’d probably want to set “.htaccess” to limit access to some kind of automated agent because if somebody hits this page manually, it’s going to dump your cache on the spot.

<?php

$baseDir = dirname(dirname(__FILE__));

include_once $baseDir . "/app/Mage.php";

$cacheType = 'full_page';

// We only really need the Mage::setRoot() function here, but the problem is
// that the underlying $cache=Mage_Core_Model_Cache function calls
// Mage::getConfig()->getOptions()in its constructor to get the cache parameters
// So we have to first use the Mage::App() function to create Mage::$_config
// We also get the needed Mage::setRoot() as a byproduct of Mage::App()  

Mage::App();

// grab a handle to the Mage::$_config variable with Mage::GetConfig(). 
$config = Mage::GetConfig();

// load the Base directories so config knows where to find the configuration files
// loads "docroot/app/etc/*" for later use with $config->getNode
$config->loadBase();

// getNode pulls the $options from the "local_xml" file 
// Change 'full_page_cache' here to 'cache' to get first level cache.
$options = $config->getNode('global/full_page_cache');
if ($options) {
  $options = $options->asArray();
} else {
  $options = array();
}

$cache = $config->getModelInstance('core/cache', $options);

$tags = $cache->cleanType($cacheType);

Mage::log('Full Page Cache Cleared via CL | Type: full_page | Tags: FPC');
echo "Cleared Full Page Cache";

?>

No comments:

Post a Comment