範例

若要將​ HTML內容型別 ​的網頁瀏覽器和CDN快取存留期增加到​ 10分鐘,而不處理過時的狀態,請遵循下列步驟:

  1. 在您的AEM專案中,從dispatcher/src/conf.d/available_vhosts目錄找出所需的vhsot檔案。

  2. 更新vhost (例如wknd.vhost)檔案,如下所示:

    <LocationMatch "^/content/.*\.(html)$">
        # Removes the response header if present
        Header unset Cache-Control
    
        # Instructs the web browser and CDN to cache the response for max-age value (600) seconds.
        Header set Cache-Control "max-age=600"
    </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環境。

不過,若要讓網頁瀏覽器和CDN快取存留期的值不同,您可以使用上述範例中的Surrogate-Control標頭。 同樣地,若要在特定日期和時間讓快取到期,您可以使用Expires標頭。 此外,使用stale-while-revalidatestale-if-error屬性,您可以控制回應內容的過時狀態處理。 AEM WKND專案有參考過時狀態處理 CDN快取設定。

同樣地,您也可以更新其他內容型別(JSON、JS、CSS和Assets)的快取標頭。

自訂Java™程式碼

此選項同時適用於AEM Publish和Author。 但是,不建議在AEM Author中啟用快取並保留預設快取行為。

若要更新快取標頭,請在自訂Java™程式碼(Sling servlet、Sling servlet篩選器)中使用HttpServletResponse物件。 一般語法如下:

// Instructs the web browser and CDN to cache the response for 'max-age' value (XXX) seconds. The 'stale-while-revalidate' and 'stale-if-error' attributes controls the stale state treatment at CDN layer.
response.setHeader("Cache-Control", "max-age=XXX,stale-while-revalidate=XXX,stale-if-error=XXX");

// Instructs the CDN to cache the response for 'max-age' value (XXX) seconds. The 'stale-while-revalidate' and 'stale-if-error' attributes controls the stale state treatment at CDN layer.
response.setHeader("Surrogate-Control", "max-age=XXX,stale-while-revalidate=XXX,stale-if-error=XXX");

// Instructs the web browser and CDN to cache the response until the specified date and time.
response.setHeader("Expires", "Sun, 31 Dec 2023 23:59:59 GMT");
recommendation-more-help