A Developer's Guide to using the Magento Cache

Magento employs caching at many levels within the system, most of which are transparent to the unwitting developer. As the complexity of your Magento tasks increase, it's inevitable that you'll eventually begin to require caching of your own.

I wrote this post for the very first time in July 2010 when Magento had released version 1.4. This article has since been updated for version 1.7.

Caching Concepts

The cache in Magento resembles many other systems in that it is a key-value store. If we wish to save $data in the cache we must address it with a name, the $key. When we later need to retrieve $data, we will do so with that same $key.

The $data stored in the cache can also be tagged with metadata. This allows for logical groupings of cache entries for mass deletion.

Controlling the Cache

For most of us, our first contact with Magento's cache comes through frustration, not understanding why one earth our perfectly formed blocks and models aren't working only to discover that 'the cache was on'.

Magento provides an interface for enabling, disabling and flushing the cache in the administration area. Each row in the "Cache Management" table represents a cache tag. When refreshing one of these rows we are deleting all cache entries with a certain tag.

The Cache API

The model controlling our interface with the caching subsystem is Mage_Core_Model_Cache. We typically access the cache through an already instantiated object accessible at Mage::app()->getCache().

The implementation of the cache is hidden from us, the interface we are given consists (pretty much entirely) of the following methods:

The actual backend used for caching can be a number of mechanisms from the built-in flat file approach (here's a more efficient algorithm) to memcached, apc, or even redis for the hardcore.

Storing Values

We'll start with storing a value.

$cache = Mage::app()->getCache();
$cache->save(date("r"), "nick_date", array("nicks_cache"), 10);
$cache->save("hello world - " . time(), "nick_helloworld", array("nicks_cache"), 60*60);

With the above example I have instructed the cache to store the value of the current date with the $key nick_date. We'll be able to refer to this date using the key at a later date. I've also stored the phrase hello world - 601992000 (for the given timestamp) with the $key nick_helloworld.

We'll ignore the tag nicks_cache for the time being, but you will notice an integer lifeTime value associated with both cache entries. This is the amount of time, in seconds, that the value will exist in cache before being deleted. By setting this value to null, or omitting the argument, the value will remain in cache until removed.

Retrieving Values

To fetch the value of a key from cache, we simply use:


If the value does not exist in cache then false will be returned.

Removing Values

To remove individual values from the cache, we use:


You'll notice that I've also applied the nicks_cache $tag to the cache entries I initially stored. By applying a tag to a set of values I am able to refer to them all at a later for mass removal. We do this by:


This will delete all cache entries with the $tag nicks_cache.