如何延长Magento 2中页面的缓存过期时间?以及如何针对Magento 2中的特定页面延长缓存过期时间?
Hey there! Let's walk through how to adjust cache expiration times in Magento 2—both for every page globally, and for specific pages individually. I’ve worked through these scenarios a bunch, so here’s what actually works:
Magento’s Full Page Cache (FPC) controls the default cache lifetime for most pages. You have two main ways to update this globally:
Option 1: Use the Magento Admin Panel
This is the simplest method for non-developers:
- Log into your Magento admin dashboard
- Navigate to
Stores > Configuration > Advanced > System > Full Page Cache - Look for the
TTL for public contentfield (TTL = Time To Live, measured in seconds) - Enter your desired value (e.g.,
86400for 24 hours,604800for 7 days) - Save the configuration, then flush the full page cache via
System > Cache Managementor run this command in your terminal:bin/magento cache:flush full_page
Option 2: Modify the env.php File (Code-Level)
If you prefer command-line or need to set this programmatically:
- Open
app/etc/env.phpin your Magento root directory - Locate or add the
cachesection, and update thelifetimevalues for bothdefaultandpage_cachefrontends:'cache' => [ 'frontend' => [ 'default' => [ 'backend' => 'Magento\Framework\Cache\Backend\File', // Or Redis/Varnish if you use them 'backend_options' => [ 'lifetime' => 86400, // Global cache lifetime in seconds 'cache_dir' => 'var/cache' ] ], 'page_cache' => [ 'backend' => 'Magento\Framework\Cache\Backend\File', 'backend_options' => [ 'lifetime' => 86400, 'cache_dir' => 'var/page_cache' ] ] ] ] - Save the file, then run:
bin/magento cache:flush
Sometimes you don’t want to change every page—here’s how to target individual pages:
For CMS Pages
- Go to
Content > Elements > Pagesin the admin, and edit the CMS page you want to adjust - Switch to the
Designtab, then expandLayout Update XML - Paste this XML snippet, replacing
86400with your desired TTL:<referenceContainer name="content"> <block class="Magento\Framework\View\Element\Page\Config" name="page_config"> <arguments> <argument name="cache_lifetime" xsi:type="number">86400</argument> </arguments> </block> </referenceContainer> - Save the page, then flush the full page cache to apply changes.
For Product/Category Pages (or Custom Module Pages)
You’ll need to edit the layout XML for the specific page type:
- Navigate to your theme’s layout directory (e.g.,
app/design/frontend/YourVendor/YourTheme/Magento_Catalog/layout/) - For a product page, edit
catalog_product_view.xml; for a category page, editcatalog_category_view.xml - Add this XML to set the page’s cache lifetime:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="root"> <arguments> <argument name="cache_lifetime" xsi:type="number">172800</argument> <!-- 48 hours, adjust as needed --> </arguments> </referenceContainer> </body> </page> - Save the file, flush the full page cache, and test the page.
For Custom Blocks
If you have a custom block that needs a longer cache lifetime, set it directly in the block class:
namespace YourVendor\YourModule\Block; class CustomBlock extends \Magento\Framework\View\Element\Template { protected function _construct() { parent::_construct(); $this->setCacheLifetime(86400); // Set TTL in seconds } }
Important Notes
- Always flush the relevant cache after making changes—otherwise, old cached content will still show up.
- Avoid setting extremely long TTLs unless you have a process to manually flush cache when content updates (e.g., when you publish a new product or edit a CMS page).
- If you use Varnish as a full-page cache, you’ll need to update Varnish’s configuration to match the TTLs you set in Magento—they work together, so mismatched settings can cause unexpected behavior.
内容的提问来源于stack exchange,提问作者rashi

