HUGO
Menu
GitHub 87548 stars Mastodon

Hugo 配置 HTTP 緩存

Hugo 配置 HTTP 緩存。

此配置僅在使用 resources.GetRemote 函數時相關。

分層緩存

Hugo 使用分層緩存系統。

H d T y T n P a c a c h | e
Dynacache
使用最近最少使用 (LRU) 驅逐策略的內存緩存。當發生更改、匹配 緩存清除 模式或內存不足時,條目將從緩存中移除。
HTTP 緩存
遠程資源的 HTTP 緩存,如 RFC 9111 中指定。當資源包含適當的 HTTP 緩存頭時可實現最佳性能。HTTP 緩存使用文件緩存來存儲和檢索緩存的資源。
文件緩存
詳見 配置文件緩存

HTTP 緩存涉及兩個關鍵方面:確定緩存哪些內容(緩存過程本身)和定義檢查更新的頻率(輪詢策略)。

HTTP 緩存

HTTP 緩存行為為一組配置的資源定義。即使配置的服務時間 (TTL) 尚未過期,過期的資源也會從文件緩存刷新。如果資源禁用了 HTTP 緩存,Hugo 將繞過緩存直接訪問文件。

這是 HTTP 緩存的默認配置:

HTTPCache:
  cache:
    for:
      excludes:
      - '**'
      includes: null
  polls:
  - disable: true
    for:
      excludes: null
      includes:
      - '**'
    high: 0s
    low: 0s
  respectCacheControlNoStoreInRequest: true
  respectCacheControlNoStoreInResponse: false
[HTTPCache]
  respectCacheControlNoStoreInRequest = true
  respectCacheControlNoStoreInResponse = false
  [HTTPCache.cache]
    [HTTPCache.cache.for]
      excludes = ['**']
  [[HTTPCache.polls]]
    disable = true
    high = '0s'
    low = '0s'
    [HTTPCache.polls.for]
      includes = ['**']
{
   "HTTPCache": {
      "cache": {
         "for": {
            "excludes": [
               "**"
            ],
            "includes": null
         }
      },
      "polls": [
         {
            "disable": true,
            "for": {
               "excludes": null,
               "includes": [
                  "**"
               ]
            },
            "high": "0s",
            "low": "0s"
         }
      ],
      "respectCacheControlNoStoreInRequest": true,
      "respectCacheControlNoStoreInResponse": false
   }
}
respectCacheControlNoStoreInRequest
New in v0.151.0
(bool) 在使用 resources.GetRemote 函數獲取遠程資源時,是否尊重服務器 Cache-Control 請求頭中的 no-store 指令。默認值是 true
respectCacheControlNoStoreInResponse
New in v0.151.0
(bool) 在使用 resources.GetRemote 函數獲取遠程資源時,是否尊重服務器 Cache-Control 響應頭中的 no-store 指令。默認值是 false
cache.for.excludes
([]string) 從緩存中排除的 glob 模式 切片。在默認配置中,HTTP 緩存排除所有文件。
cache.for.includes
([]string) 要緩存的 glob 模式 切片。
polls
輪詢配置切片。
polls.disable
(bool) 是否禁用此配置的輪詢。默認值是 true
polls.high
(string) 最大輪詢間隔,表示為 持續時間。當資源被認為穩定時使用。默認值是 0s
polls.low
(string) 最小輪詢間隔,表示為 持續時間。在最近更改後使用,並逐漸增加到 polls.high。默認值是 0s
polls.for.excludes
([]string) 從輪詢中排除的 glob 模式 切片。
polls.for.includes
([]string) 包含在輪詢中的 glob 模式 切片。

HTTP 輪詢

輪詢用於監視模式(如 hugo server)以檢測遠程資源的變化。即使禁用了 HTTP 緩存,也可以啟用輪詢。檢測到的更改會觸發使用受影響資源的頁面重建。可以為特定資源禁用輪詢,通常是那些已知是靜態的資源。

默認配置禁用所有內容:

HTTPCache:
  polls:
  - disable: true
    for:
      excludes: []
      includes:
      - '**'
    high: 0s
    low: 0s
[HTTPCache]
  [[HTTPCache.polls]]
    disable = true
    high = '0s'
    low = '0s'
    [HTTPCache.polls.for]
      excludes = []
      includes = ['**']
{
   "HTTPCache": {
      "polls": [
         {
            "disable": true,
            "for": {
               "excludes": [],
               "includes": [
                  "**"
               ]
            },
            "high": "0s",
            "low": "0s"
         }
      ]
   }
}
polls
輪詢配置切片。
polls.disable
(bool) 是否禁用此配置的輪詢。默認值是 true
polls.high
(string) 最大輪詢間隔,表示為 持續時間。當資源被認為穩定時使用。默認值是 0s
polls.low
(string) 最小輪詢間隔,表示為 持續時間。在最近更改後使用,並逐漸增加到 polls.high。默認值是 0s
polls.for.excludes
([]string) 從輪詢中排除的 glob 模式 列表。
polls.for.includes
([]string) 包含在輪詢中的 glob 模式 列表。

行為

輪詢和 HTTP 緩存的交互如下:

  • 啟用輪詢時,僅通過實際更改觸發重建,通過 eTag 更改檢測(如果服務器未提供,Hugo 會生成 MD5 哈希)。
  • 如果啟用了輪詢但禁用了 HTTP 緩存,則僅在文件緩存的 TTL 過期後檢查遠程更改(例如,maxAge10h 且輪詢間隔為 1s 是低效的)。
  • 如果啟用了輪詢和 HTTP 緩存,則即使在文件緩存的 TTL 過期之前也會檢查更改。緩存的 eTaglast-modified 值分別在 if-none-matchif-modified-since 頭中發送,並在 HTTP 304 上返回緩存的響應。

Last updated: January 1, 0001
Improve this page