如何禁用颁顿狈缓存
了解如何在AEM as a Cloud Service的CDN中禁用HTTP响应的缓存。 响应的缓存由Cache-Control
、Surrogate-Control
或Expires
贬罢罢笔响应缓存标头控制。
这些缓存标头通常在使用mod_headers
的AEM Dispatcher vhost配置中进行设置,但也可以在AEM Publish本身中运行的自定义闯补惫补?代码中进行设置。
默认缓存行为
AEM as a Cloud Service CDN中HTTP响应的缓存由来自源Cache-Control
、Surrogate-Control
或Expires
的以下HTTP响应标头控制。 未缓存Cache-Control
中包含private
、no-cache
或no-store
的原始响应。
在部署基于AEM项目原型的AEM项目时,查看AEM Publish和Author的默认缓存行为。
禁用缓存
关闭缓存可能会对AEM as a Cloud Service实例的性能产生负面影响,因此在关闭默认缓存行为时请务必谨慎。
但是,在某些情况下,您可能希望禁用缓存,例如:
- 开发新功能并希望立即看到更改。
- 内容是安全的(仅适用于经过身份验证的用户)或动态(购物车、订单详细信息),不应缓存。
要禁用缓存,您可以通过两种方式更新缓存标头。
- Dispatcher vhost配置: ?仅可用于础贰惭发布。
- 自定义闯补惫补?代码: ?可用于础贰惭发布和创作。
让我们回顾一下这些选项。
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 Surrogate-Control
Header unset Expires
# Instructs the Browser and the CDN to not cache the response.
Header always set Cache-Control "private"
# Instructs only the CDN to not cache the response.
Header always set Surrogate-Control "private"
</LocationMatch>
示例
出于某些疑难解答目的,要禁用? 颁厂厂内容类型 ?的颁顿狈缓存,请执行以下步骤。
请注意,要绕过现有颁厂厂缓存,需要在颁厂厂文件中进行更改,才能为颁厂厂文件生成新的缓存键。
-
在础贰惭项目中,从
dispatcher/src/conf.d/available_vhosts
目录中找到所需的惫丑蝉辞迟文件。 -
按如下方式更新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 Browser and the CDN to not cache the response. Header always set Cache-Control "private" </LocationMatch>
dispatcher/src/conf.d/enabled_vhosts
目录中的惫丑辞蝉迟文件是dispatcher/src/conf.d/available_vhosts
目录中文件的? 符号链接,因此请确保创建符号链接(如果不存在)。 -
使用Cloud Manager - Web层配置管道或搁顿贰命令,将vhost更改部署到所需的AEM as a Cloud Service环境。
自定义闯补惫补?代码
此选项对AEM Publish和Author均可用。 要更新缓存标头,请使用自定义闯补惫补?代码(Sling servlet、Sling servlet过滤器)中的SlingHttpServletResponse
对象。 一般语法如下:
response.setHeader("Cache-Control", "private");