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:
PHP Code:
save($value, $key, $tags = array(), $lifeTime=null)
load($key)
remove($key)
clean($tags = array()
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) tomemcached, apc, or even redis for the hardcore.
Storing Values
We'll start with storing a value.
PHP Code:
$cache = Mage::app()->getCache();
$cache->save(date("r"), "mage2x_date", array("mage2x_cache"), 10);
$cache->save("hello world - " . time(), "mage2x_helloworld", array("mage2x_cache"), 60 60);
With the above example I have instructed the cache to store the value of the current date with the $key mage2x_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 mage2x_helloworld.
We'll ignore the tag mage2x_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:
PHP Code:
$cache->load("mage2x_date");
If the value does not exist in cache then false will be returned.
Removing Values
To remove individual values from the cache, we use:
PHP Code:
$cache->remove("mage2x_date");
You'll notice that I've also applied the mage2x_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:
PHP Code:
$cache->clean(array("mage2x_cache"));
This will delete all cache entries with the $tag mage2x_cache.
View more threads in the same category:
Bookmarks