HTTP 快取

HTTP 快取可以顯著提升 Web 應用程式的效能。HTTP 快取的核心概念是 `Cache-Control` 回應標頭和後續的條件式請求標頭,例如 `Last-Modified` 和 `ETag`。`Cache-Control` 告知私有 (例如,瀏覽器) 和公有 (例如,代理伺服器) 快取如何快取和重複使用回應。`ETag` 標頭用於發出條件式請求,如果內容沒有變更,可能會產生不包含本文的 304 (NOT_MODIFIED) 回應。`ETag` 可以被視為 `Last-Modified` 標頭更進階的後繼者。

本節說明 Spring WebFlux 中可用的 HTTP 快取相關選項。

CacheControl

CacheControl 支援組態與 `Cache-Control` 標頭相關的設定,並在多個地方被接受作為引數

雖然 RFC 7234 描述了 `Cache-Control` 回應標頭的所有可能指令,但 `CacheControl` 類型採用以使用案例為導向的方法,重點放在常見情境,如下列範例所示

  • Java

  • Kotlin

// Cache for an hour - "Cache-Control: max-age=3600"
CacheControl ccCacheOneHour = CacheControl.maxAge(1, TimeUnit.HOURS);

// Prevent caching - "Cache-Control: no-store"
CacheControl ccNoStore = CacheControl.noStore();

// Cache for ten days in public and private caches,
// public caches should not transform the response
// "Cache-Control: max-age=864000, public, no-transform"
CacheControl ccCustom = CacheControl.maxAge(10, TimeUnit.DAYS).noTransform().cachePublic();
// Cache for an hour - "Cache-Control: max-age=3600"
val ccCacheOneHour = CacheControl.maxAge(1, TimeUnit.HOURS)

// Prevent caching - "Cache-Control: no-store"
val ccNoStore = CacheControl.noStore()

// Cache for ten days in public and private caches,
// public caches should not transform the response
// "Cache-Control: max-age=864000, public, no-transform"
val ccCustom = CacheControl.maxAge(10, TimeUnit.DAYS).noTransform().cachePublic()

控制器

控制器可以明確地新增對 HTTP 快取的支援。我們建議這樣做,因為資源的 `lastModified` 或 `ETag` 值需要先計算出來,才能與條件式請求標頭進行比較。控制器可以將 `ETag` 和 `Cache-Control` 設定新增到 `ResponseEntity`,如下列範例所示

  • Java

  • Kotlin

@GetMapping("/book/{id}")
public ResponseEntity<Book> showBook(@PathVariable Long id) {

	Book book = findBook(id);
	String version = book.getVersion();

	return ResponseEntity
			.ok()
			.cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
			.eTag(version) // lastModified is also available
			.body(book);
}
@GetMapping("/book/{id}")
fun showBook(@PathVariable id: Long): ResponseEntity<Book> {

	val book = findBook(id)
	val version = book.getVersion()

	return ResponseEntity
			.ok()
			.cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
			.eTag(version) // lastModified is also available
			.body(book)
}

如果與條件式請求標頭的比較指出內容沒有變更,則前面的範例會傳送包含空本文的 304 (NOT_MODIFIED) 回應。否則,`ETag` 和 `Cache-Control` 標頭會新增到回應中。

您也可以在控制器中檢查條件式請求標頭,如下列範例所示

  • Java

  • Kotlin

@RequestMapping
public String myHandleMethod(ServerWebExchange exchange, Model model) {

	long eTag = ... (1)

	if (exchange.checkNotModified(eTag)) {
		return null; (2)
	}

	model.addAttribute(...); (3)
	return "myViewName";
}
1 應用程式特定的計算。
2 回應已設定為 304 (NOT_MODIFIED)。不再進行任何處理。
3 繼續處理請求。
@RequestMapping
fun myHandleMethod(exchange: ServerWebExchange, model: Model): String? {

	val eTag: Long = ... (1)

	if (exchange.checkNotModified(eTag)) {
		return null(2)
	}

	model.addAttribute(...) (3)
	return "myViewName"
}
1 應用程式特定的計算。
2 回應已設定為 304 (NOT_MODIFIED)。不再進行任何處理。
3 繼續處理請求。

有三種變體可以針對 `eTag` 值、`lastModified` 值或兩者檢查條件式請求。對於條件式 `GET` 和 `HEAD` 請求,您可以將回應設定為 304 (NOT_MODIFIED)。對於條件式 `POST`、`PUT` 和 `DELETE`,您可以改為將回應設定為 412 (PRECONDITION_FAILED) 以防止並行修改。

靜態資源

您應該使用 `Cache-Control` 和條件式回應標頭來提供靜態資源,以獲得最佳效能。請參閱關於組態靜態資源的章節。