Contact
Site: US UK AU |
Nexcess Blog

Clearing the Cache in Magento

May 21, 2011 8 Comments

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:

$ 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

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:

/**
* 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;
}

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:

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

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:

/**
* 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);

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.

Posted in: Magento

Tags:

  • http://twitter.com/pmnordkvist Per-Mattias Nordkvis

    Great post!

    We have sometimes experienced that Magento doesn’t refresh the cache properly which forced us to Flush Cache Storage as explained in this post.

    Have you ever experienced something similar. It’s as if it isn’t able to refresh properly. It reloads way too fast. Are sure refreshing affects the slow backend as well?

  • http://twitter.com/EComCraig Craig Anderson

    Great post!
    What’s the difference between using:

    1) the Flush Magento Cache button
    vs.
    2) selecting the caches by checking the boxes, changing the Actions dropdown to “Refresh” and click Submit?

    I’m confused on this difference too, can you shed some light?

  • Jon Woolley

    I have been searching for a way to clear cache via a cron job. This would be much easier to maintain than a manual flush. BTW I am a nexcess customer.

  • http://www.facebook.com/dwightl Dwight Lee

    We just experienced some database performance issues which seemed to have cleared up after we flushed the cache on the magenta web server. I’m curious, what is the specific interaction that would cause database cpu utilization to shoot through the roof until the cache is flushed. Does the cache ever get to a point where it no longer updates and ends up with stale data?

    On a separate note, what’s the answer to Jon’s question here. I’d like to script this via cron as well. Is it a matter of running the “rm -rf var/cache/*” command (assuming I’m using the local filesystem to cache)?

    Thanks.

  • http://www.nexcess.net/ Ben

    Jon, it depends on what backend cache you’re using. If it’s just the filesystem cache, then a cron that just deletes everything under var/cache/* would work. If you’re using one of the other backend caches like Memcache, then a more programmatic solution would be needed.

    I would question the need to even do this, but Dwight’s comment reminded me of a few situations where clearing the cache fixed a seemingly unrelated issue. Still, those are one-off situations and I probably wouldn’t set up a cron to do this.

  • Audio Video van Gils

    What is the difference between pressing the “Flush Cache Storage” button and manually clearing the cache out (“rm -rf var/cache/*”)?

  • satish sojitra

    Hi Team
    Currently I am experiencing issue “Invalid id or tag : must be a string” while clicking on add to cart button or click on login in button ..Is there any solution for this ?

  • http://www.northcutt.com/ Ben Ustick

    Hello Satish, this is a support issue. If you’re a Nexcess client, please open a ticket and we can help get you sorted. If not, I’d recommend checking out the Magento forum or Magento stackexchange to troubleshoot your issue. Thanks.