May 21, 2011

Clearing the Cache in Magento
“What is the difference between the “Flush Magento Cache” and “Flush Cache Storage” buttons?”
Flush Magento Cache and Flush Cache Storage

We get this question often enough that it’s worth making a short blog post about it. In order to understand the difference between these two options, it’s important to know a little about how caching works in Magento. Specifically the concepts of ids and tagging.

Essentially, an “id” in terms of caching is simply a unique string used to identify cache records in a shared storage. A cache “tag”, is another string that’s used to classify different types of data your application is caching. In Magento, tags differentiate between the following Cache types:

  • Configuration (non-layout XML files)
  • Layouts (all those XML files under app/design/…)
  • Blocks HTML output (Page blocks like headers, footers and callouts)
  • Translations
  • Collections Data
  • EAV types and attributes (reduces some database lookups)
  • Web Services Configuration

Let’s take a look at a list of cache files from a default Magento store for an illustration:

[shell]
$ ls var/cache/mage–0
mage—1ef_DB_PDO_MYSQL_DDL_catalog_product_index_price_idx_1
mage—1ef_DB_PDO_MYSQL_DDL_core_config_data_1
mage—1ef_LAYOUT_0183D2D163E71FE45BB4CE3F4045A71BD
mage—1ef_LAYOUT_0659E64C667F785D2436DB04EBCBEE12E
mage—1ef_LAYOUT_088A9AF9EA75F3D59B57387F8E9C7D7A6
mage—1ef_LAYOUT_0956CDEF59F213D48A2D1218CC2CD1E96
mage—1ef_LAYOUT_1013A059DA3EFFB6F31EB8ABA68D0469E
mage—1ef_LAYOUT_12D7604E9632FF8D14B782A248FCBD2E7
mage—1ef_LAYOUT_14E2F46FB273D9CEA54FDD1B14EB28645
mage—1ef_LAYOUT_16CD0CCB23CB5ABE6844B7E3241F0A751
mage—1ef_LAYOUT_1DC0705D40BBC39A32179EE8A85BEF5D7
mage—1ef_Zend_LocaleC_en_US_day_gregorian_format_wide_wed
mage—1ef_Zend_LocaleC_en_US_month_gregorian_format_wide_5
[/shell]

As you can see, there’s a distinct pattern to the cache file names. Magento’s implementation of the Zend Cache prefixes all files with “mage”, followed by an id prefix (technically: the first 3 characters from an md5 hash of the path to your app/etc/ directory), followed by the cache Tag and some other identifiers. The whole thing makes up the unique id for the specific cache entry.

With this information at hand, we can better understand what happens when you clear the Magento cache in the back end.

“Flush Magento Cache”

When you click “Flush Magento Cache”, the Cache Controller calls the “flushSystemAction()” method. This function, in turn, calls the “cleanCache()” method of the Mage_Core_Model_App object, which subsequently calls the “clean($tags)” method in the Mage_Core_Model_Cache object. Here’s where the differences between the two methods come into play. Let’s take a look at that function definition:

[php]
/**
* Clean cached data by specific tag
*
* @param array $tags
* @return bool
*/
public function clean($tags=array())
{
$mode = Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG;
if (!empty($tags)) {
if (!is_array($tags)) {
$tags = array($tags);
}
$res = $this->_frontend->clean($mode, $this->_tags($tags));
} else {
$res = $this->_frontend->clean($mode, array(Mage_Core_Model_App::CACHE_TAG));
$res = $res && $this->_frontend->clean($mode, array(Mage_Core_Model_Config::CACHE_TAG));
}
return $res;
}
[/php]

This method is calling on the Zend Cache object, which we’ll look at in a moment. But first I want to point out that this method is cleaning all cache files matching an array of tags, or, if empty, the tags defined in Mage_Core_Model_App and Mage_Core_Model_Config. This does not clean the entire cache!

Before we look at the Zend_Cache “clean()” function definition, let’s see what happens when we click the other button, “Flush Cache Storage”.

“Flush Cache Storage”

When you click this button, the Cache Controller calls the “flushAllAction()” method, rather than the “flushSystemAction()” method we saw above. This method, in turn, gets the Cache Object from Mage_Core_Model_App similar to the above method, but instead of calling “clean($tags)” on the Cache Object, it calls “flush()”. Let’s look at that definition briefly:

[php]
/**
* Clean cached data by specific tag
*
* @return bool
*/
public function flush()
{
$res = $this->_frontend->clean();
return $res;
}
[/php]

Well, look here! This method is calling the same Zend Cache method as the method above, except for one major difference: it’s not passing any tags!. Now let’s take a look at the Zend_Cache_Core::clean() method to see what happens when you don’t tell it what to flush:

[php]
/**
* Clean cache entries
*
* Available modes are :
* ‘all’ (default) => remove all cache entries ($tags is not used)
* ‘old’ => remove too old cache entries ($tags is not used)
* ‘matchingTag’ => remove cache entries matching all given tags
* ($tags can be an array of strings or a single string)
* ‘notMatchingTag’ => remove cache entries not matching one of the given tags
* ($tags can be an array of strings or a single string)
* ‘matchingAnyTag’ => remove cache entries matching any given tags
* ($tags can be an array of strings or a single string)
*
* @param string $mode
* @param array|string $tags
* @throws Zend_Cache_Exception
* @return boolean True if ok
*/
public function clean($mode = ‘all’, $tags = array())
{
if (!$this->_options[‘caching’]) {
return true;
}
if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
Zend_Cache::CLEANING_MODE_OLD,
Zend_Cache::CLEANING_MODE_MATCHING_TAG,
Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
Zend_Cache::throwException(‘Invalid cleaning mode’);
}
self::_validateTagsArray($tags);
return $this->_backend->clean($mode, $tags);
[/php]

The documentation in this function definition is pretty good. You can see here that while the first button – “Flush Magento Cache” – tries to invalidate all cached assets matching any tag passed in the $tags array, “Any” here is but a limited subset of the items stored in the cache. However, when you call the “clean()” method without any parameters, as the “Flush Cache Storage” button does, the default behavior is to remove all cache entries irrespective of tags.

This may seem trivial to those of you using the default filesystem cache with Magento. You can just go in and manually “rm -rf var/cache/*” to clear the cache out. But those of you using the alternate cache types (xcache, memcached, apc, db, sqlite), the “Flush Magento Cache” may not be doing what you want it to do. So, when all else fails and you want to be sure the cache is totally clear, be sure to click on “Flush Cache Storage”.

One caveat before I say farewell, if you are using one of the shared storage cache types – for example two different apps using the same memcached instance – clicking on “Flush Cache Storage” might remove the cache entries for that other application as well. This may not be what you want, so just take heed.

Nexcess
Nexcess

Nexcess, the premium hosting provider for WordPress, WooCommerce, and Magento, is optimized for your hosting needs. Nexcess provides a managed hosting infrastructure, curated tools, and a team of experts that make it easy to build, manage, and grow your business online. Serving SMBs and the designers, developers, and agencies who create for them, Nexcess has provided fully managed, high-performance cloud solutions for more than 22 years.


We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.