Clearing the Cache in Magento

Clearing the Cache in Magento

May 21

  • Created: May 21, 2011 10:28 AM

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:

<br />
$ ls var/cache/mage--0<br />
mage---1ef_DB_PDO_MYSQL_DDL_catalog_product_index_price_idx_1<br />
mage---1ef_DB_PDO_MYSQL_DDL_core_config_data_1<br />
mage---1ef_LAYOUT_0183D2D163E71FE45BB4CE3F4045A71BD<br />
mage---1ef_LAYOUT_0659E64C667F785D2436DB04EBCBEE12E<br />
mage---1ef_LAYOUT_088A9AF9EA75F3D59B57387F8E9C7D7A6<br />
mage---1ef_LAYOUT_0956CDEF59F213D48A2D1218CC2CD1E96<br />
mage---1ef_LAYOUT_1013A059DA3EFFB6F31EB8ABA68D0469E<br />
mage---1ef_LAYOUT_12D7604E9632FF8D14B782A248FCBD2E7<br />
mage---1ef_LAYOUT_14E2F46FB273D9CEA54FDD1B14EB28645<br />
mage---1ef_LAYOUT_16CD0CCB23CB5ABE6844B7E3241F0A751<br />
mage---1ef_LAYOUT_1DC0705D40BBC39A32179EE8A85BEF5D7<br />
mage---1ef_Zend_LocaleC_en_US_day_gregorian_format_wide_wed<br />
mage---1ef_Zend_LocaleC_en_US_month_gregorian_format_wide_5<br />

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:

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

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:

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

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:

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

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.

  • 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/*”)?