You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何延长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:

1. Extend Cache Expiration for All Magento 2 Pages

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 content field (TTL = Time To Live, measured in seconds)
  • Enter your desired value (e.g., 86400 for 24 hours, 604800 for 7 days)
  • Save the configuration, then flush the full page cache via System > Cache Management or 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.php in your Magento root directory
  • Locate or add the cache section, and update the lifetime values for both default and page_cache frontends:
    '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
    
2. Extend Cache Expiration for Specific Pages

Sometimes you don’t want to change every page—here’s how to target individual pages:

For CMS Pages

  • Go to Content > Elements > Pages in the admin, and edit the CMS page you want to adjust
  • Switch to the Design tab, then expand Layout Update XML
  • Paste this XML snippet, replacing 86400 with 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:

  1. Navigate to your theme’s layout directory (e.g., app/design/frontend/YourVendor/YourTheme/Magento_Catalog/layout/)
  2. For a product page, edit catalog_product_view.xml; for a category page, edit catalog_category_view.xml
  3. 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>
    
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 19:13:16