如何停用CDN快取

瞭解如何在AEM as a Cloud Service的CDN中停用HTTP回應的快取。 回應快取是由Cache-ControlSurrogate-ControlExpires HTTP回應快取標頭所控制。

這些快取標頭通常在使用mod_headers的AEM Dispatcher vhost設定中設定,但也可以在AEM Publish本身中執行的自訂Java™程式碼中設定。

預設快取行為

在部署基於AEM Project Archetype的AEM專案時,檢閱AEM Publish和Author的預設快取行為。

停用快取

關閉快取可能會對AEM as a Cloud Service執行個體的效能產生負面影響,因此在關閉預設快取行為時請務必謹慎。

不過,在某些情況下,您可能會想要停用快取,例如:

  • 正在開發新功能,並且想要立即看到變更。
  • 內容安全(僅供驗證的使用者使用)或動態(購物車、訂單詳細資料)且不應快取。

若要停用快取,您可以使用兩種方式更新快取標頭。

  1. Dispatcher vhost設定: ​僅適用於AEM Publish。
  2. 自訂Java™程式碼: ​可同時用於AEM Publish和Author。

讓我們檢閱這些選項中的每一個。

Dispatcher vhost設定

此選項是停用快取的建議方法,但此選項僅適用於AEM Publish。 若要更新快取標頭,請使用Apache HTTP伺服器的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目錄中檔案的​ symlink,因此請務必建立symlink (若不存在)。

  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