如何禁用CDN缓存

了解如何在AEM as a Cloud Service的CDN中禁用HTTP响应的缓存。 响应的缓存由Cache-ControlSurrogate-ControlExpires HTTP响应缓存标头控制。

这些缓存标头通常在使用mod_headers的AEM Dispatcher vhost配置中进行设置,但也可以在AEM Publish本身中运行的自定义Java™代码中进行设置。

默认缓存行为

在部署基于AEM项目原型的AEM项目时,查看AEM Publish和作者的默认缓存行为。

禁用缓存

关闭缓存可能会对AEM as a Cloud Service实例的性能产生负面影响,因此在关闭默认缓存行为时请务必谨慎。

但是,在某些情况下,您可能希望禁用缓存,例如:

  • 开发新功能并希望立即看到更改。
  • 内容是安全的(仅适用于经过身份验证的用户)或动态(购物车、订单详细信息),不应缓存。

要禁用缓存,您可以通过两种方式更新缓存标头。

  1. Dispatcher vhost配置: ​仅适用于AEM Publish。
  2. 自定义Java™代码: ​适用于AEM Publish和Author。

让我们回顾一下这些选项。

Dispatcher vhost配置

此选项是禁用缓存的推荐方法,但它仅适用于AEM Publish。 要更新缓存标头,请使用Apache HTTP Server的vhost文件中的mod_headers模块和<LocationMatch>指令。 一般语法如下:

<LocationMatch "$URL$ || $URL_REGEX$">
    # Removes the response header of this name, if it exists. If there are multiple headers of the same name, all will be removed.
    Header unset Cache-Control
    Header unset Expires

    # Instructs the CDN to not cache the response.
    Header set Cache-Control "private"
</LocationMatch>

示例

出于某些疑难解答目的,要禁用​ CSS内容类型 ​的CDN缓存,请执行以下步骤。

请注意,要绕过现有CSS缓存,需要在CSS文件中进行更改,才能为CSS文件生成新的缓存键。

  1. 在AEM项目中,从dispatcher/src/conf.d/available_vhosts目录中找到所需的vhsot文件。

  2. 按如下方式更新vhost (例如wknd.vhost)文件:

    code language-none
    <LocationMatch "^/etc.clientlibs/.*\.(css)$">
        # Removes the response header of this name, if it exists. If there are multiple headers of the same name, all will be removed.
        Header unset Cache-Control
        Header unset Expires
    
        # Instructs the CDN to not cache the response.
        Header set Cache-Control "private"
    </LocationMatch>
    

    dispatcher/src/conf.d/enabled_vhosts目录中的vhost文件是dispatcher/src/conf.d/available_vhosts目录中文件的​ 符号链接,因此请确保创建符号链接(如果不存在)。

  3. 使用Cloud Manager - Web层配置管道RDE命令,将vhost更改部署到所需的AEM as a Cloud Service环境。

自定义Java™代码

此选项对AEM Publish和Author均可用。 要更新缓存标头,请使用自定义Java™代码(Sling servlet、Sling servlet过滤器)中的SlingHttpServletResponse对象。 一般语法如下:

response.setHeader("Cache-Control", "private");
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69