HTTP 快取

HTTP 快取可以顯著提升 Web 應用的效能。HTTP 快取圍繞 Cache-Control 響應頭以及隨後的條件請求頭,如 Last-ModifiedETagCache-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 快取的顯式支援。我們建議這樣做,因為資源的 lastModifiedETag 值需要在與條件請求頭進行比較之前計算出來。控制器可以向 ResponseEntity 新增 ETagCache-Control 設定,如下例所示:

  • 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) 響應。否則,會將 ETagCache-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 值或兩者都對照。對於條件 GETHEAD 請求,可以將響應設定為 304 (NOT_MODIFIED)。對於條件 POSTPUTDELETE,可以將其響應設定為 412 (PRECONDITION_FAILED) 以防止併發修改。

靜態資源

為了獲得最佳效能,應使用 Cache-Control 和條件響應頭來提供靜態資源。請參閱配置靜態資源一節。