MAGENTO

Caching Blocks in Magento

magentoEven 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(){  parent::__construct();  $this->setTemplate('company/home-product-list.phtml');  // Block caching setup  $this->addData(array(    'cache_lifetime'=> 86400, // (seconds) data lifetime in the cache    'cache_tags' => array(      Mage_Core_Model_Store::CACHE_TAG,      Mage_Cms_Model_Block::CACHE_TAG,      // tag (can be applied to the number of various blocks)      // using the tag we can clear the cache of several blocks at a time      'tag_home_product_list'    ),    '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.

ATTENTION!!

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 IDMage::app()->removeCache( ‘MY_HOME_PRODUCT_LIST’ );
// Cleaning cache data using tagMage::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,NEKLO LLC

Related posts

MAGENTO
Magento in 2021 and Beyond: Magento Experts Share Predictions
Two experts from the NEKLO ecommerce team, contributing to our expertise as a Magento development company, share their thoughts on the potential of their favorite and one of the most popular ecommerce platforms on the market today. Find below expert quotes about Magento development, Magento services, and Magento partners from our Senior Magento Developer Michail…
author's avatar
Nadya Bakhur
Researcher, Content Writer
TECHNICAL
App Development Cost Breakdown: How Much Does It Cost to Design an App
There is a wide price range within custom software development and mobile app development today. This article focuses on the factors that affect the final average app development cost of a project. It will help you negotiate the price with any application development specialist googled through the “app developers near me” query and understand how…
author's avatar
Dasha Korsik
Content Team Lead
TECHNICAL
Build vs Buy Software: Pros & Cons of Custom Software Development
In today’s digital world, almost every business, regardless of its size or niche, needs a software solution to manage data, improve customer experience and update internal processes. A dizzying array of business software programs is already available on the market and there is always a prospect to build a custom software solution. “Buy vs build…
author's avatar
Nadya Bakhur
Researcher, Content Writer
INSIGHTS
NEKLO DIGEST #5: Is Dropshipping Worth It? Ecommerce Strategy Tips
Starting an ecommerce activity seems the right choice for many business owners today. However, many of them are puzzled about existing ecommerce business ideas and aren’t sure which one to follow.  When the choice is difficult, they come to our ecommerce development company for a custom software solution or professional consultancy. Additionally, we provide the…
author's avatar
Dasha Korsik
Content Team Lead
MAGENTO
Magento Big Data Solutions: How Big Data Projects Change eCommerce?
If you rely on data and analytics, the chance you will be disappointed is minimized. In this article, we’re going to overview why and how companies use big data within their ecommerce initiatives. What’s more, NEKLO team wants to make it certain for you that one of the key benefits of merging Magento web development…
author's avatar
Nadya Bakhur
Researcher, Content Writer