How to cache custom data into Magento

Caching data in Magento can have a significant impact on the performance of your site. Here’s how a few lines of code you can easily cache any data type.

// ...
$cacheId = 'my_cache_id';
if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
    $data = unserialize($data);
} else {
    $data = $this->getMyHeavyData();
    Mage::app()->getCache()->save(serialize($data), $cacheId);

And that’s all ;)

4 Responses to “How to cache custom data into Magento”

  1. mohammed abdelrahman says:


  2. Vlad N. says:

    Thanks dude, this helped me alot.

  3. Luis says:


    Where do you use this lines?

    THank you for your answer.

  4. How can one check if a cache key is enabled or disabled? I want to tag my caching onto BLOCK_HTML caching and have it only happen if that is enabled.

Leave a Reply

* Required fields