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。