Caching Blocks in Magento


Even quite experienced developers don’t use block caching in their work with Magento. You can also meet a lot of paid Magento themes and modules where this kind of caching is not used at all.

But in fact, using this standard feature is not hard. This feature is part of any Magento block (inherited from Mage_Core_Block_Abstract).

There is a simple example of its usage. For example, on the Home page, we need to display a collection of products which is sorted according to certain criteria. The data on the page should be updated only once a day. We need to make changes only in two places in the code:

public function __construct()
  // Block caching setup
    'cache_lifetime'=> 86400, // (seconds) data lifetime in the cache
    'cache_tags' => array(
      // tag (can be applied to the number of various blocks)
      // using the tag we can clear the cache of several blocks at a time
    'cache_key' => 'MY_HOME_PRODUCT_LIST',

As we see from the example data is kept in cache separately for the store ID, for access using SSL, for access without SSL, for each Package, for each theme and for logged in user and not logged users.

For example, if your Magento uses two stores then the number of copies of the block in cache will be two. In case you use two different Magento themes then the number of copies of the block in cache will be four.


Be careful with the attempt to cache the standard blocks like catalogue/product_view. The thing is that these blocks are used for rendering several parts of Product Page. You should understand the following:

1) Where this block is used when the page is rendering.

2) What page parts you want to be cached.

Only after these steps, you can determine block cache ID and Tag. If you don’t complete these 2 steps you may put in cache page parts that you did NOT plan.

Cache cleaning

Sometimes you need to clean the cache without waiting till date the cache is expired. There are 2 ways to clear cache:

// Cleaning cache data using ID

Mage::app()->removeCache( ‘MY_HOME_PRODUCT_LIST’ );

// Cleaning cache data using tag

Mage::app()->cleanCache( array(‘tag_home_product_list’, ‘tag2’, ‘tag3’) );

Always use the block caching where it is reasonable. Good Luck!

Anatol Kravchenko,

The lead of PHP team,


Subscribe to our newsletter with new materials

Related posts