Increasing Cache Expiration Time for Specific Pages in Magento 2
Speed is paramount in e-commerce, and optimizing your cache can significantly improve loading times. However, by default, Magento 2’s cache expires quicklyâsometimes as little as every hour. This isn’t ideal for pages with static content like product descriptions or contact information.
In this guide, we’ll show you how to customize the cache expiration time for specific pages using Magento 2’s configuration and layout XML files. Let’s dive in!
Understanding Magento 2 Cache Types
Before we begin, it’s essential to understand the different types of cache in Magento 2:
- Config cache: Stores configuration data.
- Layout cache: Contains compiled layouts for your pages.
- Block HTML output cache: Caches rendered block HTML.
- collections: Caches database query results.
For our use case, we’ll focus on the layout and block HTML caches.
Increasing Cache Expiration Time
Step 1: Accessing Magento 2 Admin Panel
First, log in to your Magento 2 admin panel at http://your-store.com/admin/.
Step 2: Navigating to Stores Configuration
Navigate through the following menu:
Stores -> Configuration
Expand the Advanced section on the left sidebar and click on System.
Step 3: Customizing Cache Settings
Find the Cache Management section. Here, you’ll see various cache types along with their status (enabled or disabled) and expiration time.
To increase the cache lifetime for specific pages:
- For layout cache: Set
Layoutto your desired duration (e.g., 86400 seconds = 24 hours). - For block HTML output cache: Set
Block HTML outputto your preferred duration.
Click Save Config at the top right corner of the page.
Step 4: Flushing Cache Manually
After updating the configuration, you’ll need to flush the cache:
- From the admin panel menu, go to System -> Cache Management.
- Check all boxes in the Select Cache Type column.
- Click on the Flush All Magento Caches button.
Customizing Cache Expiration via Layout XML
For more granular control, you can define cache lifetime directly using layout XML files. Here’s how:
- Locate your page or block in
app/design/frontend/{Vendor}/{Theme}/layout. - Open the relevant layout file.
- Add the
<referenceBlock ...>with cache lifetime attributes as needed.
For example, to set a 4-hour cache lifetime for the product view page:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="catalog.product.view" cache_lifetime="14400"/>
</body>
</page>
Replace 14400 with the desired lifetime in seconds.
Best Practices
- Be cautious: Longer cache lifetimes mean your content may not update immediately after changes.
- Use CDN wisely: If you have a CDN, configure it to respect Magento’s cache headers.
- Monitor performance: Track loading times and adjust accordingly using tools like Google Lighthouse or GTmetrix.
Conclusion
Increasing cache expiration time for specific pages in Magento 2 is a simple yet powerful optimization. By following this guide and customizing your configuration files, you can improve the performance of your store significantly without compromising content freshness too much.
Remember, the key lies in finding the right balance between speed and up-to-date data. Regularly monitor your cache settings to ensure they align with your business needs.
If you found this tutorial helpful, check out our comprehensive guide on Magento 2 Performance Optimization for more advanced techniques!
Happy optimizing!