Thursday, 15 November 2012

Block Level Caching in Magento

Most of the Magento site falls in slower side. So Magento Caching plays a big role to speed up the site. However, here we discussing the Caching on the Block level. How to cache a particular block part.
You only need to add below code in the block class:

protected function _construct()
{
$this->addData(array(
'cache_lifetime' => 3600,
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
'cache_key' => "PRODUCT" . $this->getProduct()->getId(),
));
}


cache_lifetime – you can either specify number in seconds or false. If set to false, then cache will never expire.

cache_tags – cache tag is used to group the cache which means the same cache can be cleared or flushed at the same time. In the above example we are using product cache which mean this cache will be deleted as soon as any product is saved in admin

cache_key - It creates a unique cache key with combination of product Id and some static string.

Don't Forget to check the "Block html Output Cache" enabled

No comments :

Post a Comment