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 值需要計算,然後才能與條件請求頭進行比較。控制器可以將 ETagCache-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) 響應,其中包含空正文。否則,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 和條件響應頭來提供靜態資源,以實現最佳效能。請參閱配置靜態資源一節。

© . This site is unofficial and not affiliated with VMware.