Caching is one of the most underrated performance boosters in Magento 2. It directly impacts page speed, user experience, and server load. When you add block to cache in Magento 2, you’re telling the system to reuse stored content instead of regenerating it each time a user visits.
That means faster rendering, smoother navigation, and better performance under traffic spikes.
As we all know there are many cases in Magento 2 development where the product listing is too big which affects page load time. Now you can decrease the page load time by adding that specific block to the cache. Below are the different ways to achieve your goal:
Add a block to cache from layout xml:
Add below _construct() in your respected block file and specify cache_lifetime and a unique cache_tags cache identifier.
{{block name="explore-color-section" class="Companyname\Packagename\Block\Product\CustomProductCollection" product_id="51,52,53,54,55,56,57,58,59,60,40, 41, 42, 43, 44, 45, 46, 47, 70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90 ,91,92,93,94,95,96,97,98,99,100,101,102,103" cacheable="true" cache_lifetime="86400" template="Magento_Theme::html/explore_color_code.phtml"}}
1. Create or update your block class
Add the _construct () given below in your respected block file and specify cache_lifetime and a unique cache_tags cache identifier.
/**
* {@inheritdoc}
*/
protected function _construct()
{
parent::_construct();
$this->addData([
'cache_lifetime' => 86400,
'cache_tags' => ["explore_your_colors_ilnp"]]);
}Note: Here its _construct not __construct
2. Add a block having pagination to cache
Add the _construct() given below in your respected block file and specify cache_lifetime and a unique cache_tags cache identifier. Use this snippet inside your class:
protected function _construct()
{
parent::_construct();
$this->addData([
'cache_lifetime' => 86400,
'cache_tags' => [\Magento\Store\Model\Store::CACHE_TAG, \Magento\Cms\Model\Block::CACHE_TAG],
'cache_key' => "explore_your_colors_ilnp_".$this->request->getPost('page_number')
]
);
}3. Clear and refresh the cache
After saving your block, run these CLI commands:
php bin/magento cache:clean
php bin/magento cache:flushWhat Is a Block Cache in Magento 2?
In Magento 2, every page is made up of blocks, small components like headers, footers, banners, or product listings. A block cache stores the HTML output of these blocks. When the same page is requested again, Magento serves the cached version instead of rebuilding it from scratch.
This reduces load times significantly and saves system resources. Developers often configure caching for custom blocks to improve overall site speed and stability.
Why You Should Cache Blocks
Here’s why caching matters more than you think:
- Improves performance: Cached blocks reduce server-side processing time.
- Boosts scalability: Handles high-traffic loads without crashing.
- Enhances SEO: Faster load speeds improve Google Core Web Vitals.
- Optimizes user experience: Visitors get content instantly.
If you’ve been struggling with slow page loads or high bounce rates, caching might be the fix you’ve been missing.
Final Thoughts
When you add block to cache in Magento 2, you’re not just improving speed; you’re building a foundation for scalability and stability. Caching cuts redundant processing, helps pages load faster, and ultimately enhances the customer experience.
Handled properly, block caching can make your Magento store run like a well-oiled machine. So take the time to set it up right, monitor results, and tweak configurations for maximum efficiency.
Related resources:
Post a Comment
Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️