HTTP 快取
HTTP 快取可以顯著提高 Web 應用程式的效能。HTTP 快取圍繞著 Cache-Control 響應頭,以及隨後的條件請求頭(例如 Last-Modified 和 ETag)。Cache-Control 告知私有(例如,瀏覽器)和公共(例如,代理)快取如何快取和重用響應。ETag 頭用於發出條件請求,如果內容未更改,可能會導致 304 (NOT_MODIFIED) 且沒有正文。ETag 可以看作是 Last-Modified 頭更復雜的繼任者。
本節描述了 Spring Web MVC 中可用的 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()
WebContentGenerator 也接受一個更簡單的 cachePeriod 屬性(以秒為單位定義),其工作方式如下:
-
值為
-1不生成Cache-Control響應頭。 -
值為
0透過使用'Cache-Control: no-store'指令阻止快取。 -
值為
n > 0透過使用'Cache-Control: max-age=n'指令將給定響應快取n秒。
控制器
控制器可以新增對 HTTP 快取的顯式支援。我們建議這樣做,因為資源的 lastModified 或 ETag 值需要在與條件請求頭進行比較之前進行計算。控制器可以向 ResponseEntity 新增 ETag 頭和 Cache-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) 響應。否則,ETag 和 Cache-Control 頭將新增到響應中。
您也可以在控制器中進行條件請求頭檢查,如下例所示:
-
Java
-
Kotlin
@RequestMapping
public String myHandleMethod(WebRequest request, Model model) {
long eTag = ... (1)
if (request.checkNotModified(eTag)) {
return null; (2)
}
model.addAttribute(...); (3)
return "myViewName";
}
| 1 | 應用程式特定計算。 |
| 2 | 響應已設定為 304 (NOT_MODIFIED) — 無需進一步處理。 |
| 3 | 繼續請求處理。 |
@RequestMapping
fun myHandleMethod(request: WebRequest, model: Model): String? {
val eTag: Long = ... (1)
if (request.checkNotModified(eTag)) {
return null (2)
}
model[...] = ... (3)
return "myViewName"
}
| 1 | 應用程式特定計算。 |
| 2 | 響應已設定為 304 (NOT_MODIFIED) — 無需進一步處理。 |
| 3 | 繼續請求處理。 |
有三種檢查條件請求的方式:針對 eTag 值、lastModified 值或兩者。對於條件 GET 和 HEAD 請求,您可以將響應設定為 304 (NOT_MODIFIED)。對於條件 POST、PUT 和 DELETE,您可以將響應設定為 412 (PRECONDITION_FAILED),以防止併發修改。
靜態資源
您應該使用 Cache-Control 和條件響應頭來提供靜態資源,以獲得最佳效能。請參閱配置靜態資源一節。
ETag 過濾器
您可以使用 ShallowEtagHeaderFilter 新增從響應內容計算的“淺層” eTag 值,從而節省頻寬而不是 CPU 時間。請參閱淺層 ETag。