Magento Cache

Magento uses cache at different levels in the system. These can be seen in the admin area under System -> Cache Management.

Magento Cache

The main model responsible for Magento’s cache is the Mage_Core_Model_Cache class. You can retrieve an instance of the class using the below line of code.


The following methods can be found in the class.

  • load() – Load data from the cache by ID.
  • save() – Save data to the cache.
  • remove() – Remove data from the cache by ID.
  • clean() – Clean cache data by tag.
  • flush() – Flush cached data.

We can save our own data in the cache by doing the following:

$cache = Mage::app()->getCache();
$cache->save("My cached text", "custom_cache", array(), 60*60*24);
echo $cache->load("custom_cache");

The save() method takes four parameters: $data, $id, $tags and $lifetime.

The $lifetime, is of course the amount of time in seconds that the value will exist in the cache before being removed. Setting this value to null or omitting the parameter, the value will remain in the cache until removed.

public function save($data, $id, $tags = array(), $lifeTime = null)
    if ($this->_disallowSave) {
        return true;

     * Add global magento cache tag to all cached data exclude config cache
    if (!in_array(Mage_Core_Model_Config::CACHE_TAG, $tags)) {
        $tags[] = Mage_Core_Model_App::CACHE_TAG;
    return $this->getFrontend()->save((string)$data, $this->_id($id), $this->_tags($tags), $lifeTime);

The load() method just takes the one argument which is the ID of the data saved in the cache.

public function load($id)
    return $this->getFrontend()->load($this->_id($id));

There are two buttons on the cache management page that can be used to flush the cache. One is Flush Magento Cache and the other is Flush Cache Storage.

Flush Magento Cache will flush the cache storage that Magento is aware of.

public function flushSystemAction()
    $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The Magento cache storage has been flushed."));

<code>Flush Cache Storage</code> flushes cache including non-Magento cache such as APC.

[php title="file: app/code/core/Mage/Adminhtml/CacheController.php
public function flushAllAction()
    $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The cache storage has been flushed."));

Cache types, like with most things in Magento, are configured in Magento’s config.xml files.

                <config translate="label,description" module="core">
                    <description>System(config.xml, local.xml) and modules configuration files(config.xml).</description>
                <layout translate="label,description" module="core">
                    <description>Layout building instructions.</description>
                <block_html translate="label,description" module="core">
                    <label>Blocks HTML output</label>
                    <description>Page blocks HTML.</description>
                <translate translate="label,description" module="core">
                    <description>Translation files.</description>
                <collections translate="label,description" module="core">
                    <label>Collections Data</label>
                    <description>Collection data files.</description>

This means that if developing a custom cache module, you would need to declare your cache type similarly to how Magento declare theirs.

Note: This article is based on Magento Community/Open Source version 1.9.