Magento development and cache

Magento can be setup to use the various cache methods. These include sqlite, memcached, apc, eaccelerator, database and file types. The Magento cache can be found through the Mage_Core_Model_Cache model.

Let’s access the cache.

$cached = Mage::app()->getCache();

The methods available to the object $cached are listed below:

  • load($id)
  • save($data, $id, $tags=array(), $lifeTime=null)
  • remove($id)
  • clean($tags=array())
  • flush()

Saving data to cache

To save data to cache we call the following:

$cached->save("Sarg", "myName", array("cached_name"), 7200);

We are storing the data “Sarg”. It is identified by the key “myName”. We are also giving it the cache tag “cached_name”. Tags are used to group data together. The lifetime of the cache is 7200 seconds.

 Reading cached data

To read the cached data, call the load method passing the cache key/id.


 Clearing cache

We can also clear the cache by it’s key/id.


Clearing cache by it’s tag

We can also clear cache by tags. This clears all cache matching an array of tags. To clear all data with the tag “cached_name”.


Flush cache

This clears all cache.


If we look at the cache model, we can see the following for the clean method:

public function clean($tags=array())
    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;

And for flush method:

public function flush()
    $res = $this->_frontend->clean();
    return $res;

We can see that the flush method actually calls the clean method without passing any tags. So calling:


will also clear the cache.

Was this helpful? Do you want to add anything more to this. Send us a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *