Custom Cache in Magento
Every now and then, every developer has to deal with a huge amount of data that is slowing down their website and can’t be worked around. Fortunately, Magento has a caching system that allows us to cache our data in an easy and convenient way.
The model that we’re going to discuss in this article is Mage::getModel(‘core/cache’), which has 4 main methods:
They are pretty much self-explanatory, but it’s good to specify what the $key and $tags variables represent. The $key variable is an identifier for the cached data and $tags aggregates the $keys. They allow us to either clear all related caches or choose the ones want we want to flush.
Since Magento already has a bunch of cache tags by default, we don’t always have to write our own modules (as long as assigning our custom cache to some of the default tags is reasonable) and we can jump right into action. Take a look at the example below:
Please note that we need to serialize the data so that can be stored as a string.
If we want to clear our example_id cache, we can either do it simply from the admin panel in Cache Storage Management and then refresh “Blocks HTML output” or do it programmatically using remove($cacheId) or clean(array($cacheTag)).
But what should we do if we want to add our own cache tag? Guess what, it’s just a matter of adding a couple of lines to the config.xml file of the module:
As simple as that! Now, depending on your needs you can easily enable, disable or refresh your cache from Cache Storage Management.
This tutorial is based on the article by Andrei Danilchyk, which can be found under the following address: http://blog.belvg.com/using-default-magento-cache.html.