此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

HTTP 缓存

HTTP 缓存可以显着提高 Web 应用程序的性能。HTTP 缓存围绕Cache-Controlresponse 标头,随后的条件请求标头(例如Last-ModifiedETag).Cache-Control建议私有(例如浏览器)和公共(例如代理)缓存,了解如何缓存和重用响应。 一ETag标头发出条件请求,可能导致没有正文的 304 (NOT_MODIFIED),如果内容没有改变。ETag可以看作是更复杂的继任者 这Last-Modified页眉。spring-doc.cadn.net.cn

本节介绍 Spring Web MVC 中可用的 HTTP 缓存相关选项。spring-doc.cadn.net.cn

CacheControl

CacheControl提供支持配置与Cache-Control标头,并被接受为参数在许多地方:spring-doc.cadn.net.cn

虽然 RFC 7234 描述了所有可能的指令Cache-Controlresponse 标头,CacheControl类型采用面向用例的方法,专注于常见场景:spring-doc.cadn.net.cn

// 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属性(以秒为单位定义),该属性工作原理如下:spring-doc.cadn.net.cn

控制器

控制器可以添加对 HTTP 缓存的显式支持。我们建议这样做,因为lastModifiedETag需要先计算资源的值,然后才能对其进行比较与条件请求标头。控制器可以添加ETagheader 和Cache-Controlsettings 设置为ResponseEntity,如以下示例所示:spring-doc.cadn.net.cn

@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标头将添加到响应中。spring-doc.cadn.net.cn

您还可以对控制器中的条件请求标头进行检查,如以下示例所示:spring-doc.cadn.net.cn

@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 继续请求处理。

有三种变体用于检查条件请求eTaglastModified值,或两者。对于有条件的GETHEAD请求,您可以将响应设置为 304 (NOT_MODIFIED)。对于有条件的POST,PUTDELETE,您可以改为设置响应 设置为 412 (PRECONDITION_FAILED),以防止并发修改。spring-doc.cadn.net.cn

静态资源

您应该使用Cache-Control和条件响应标头 以获得最佳性能。请参阅有关配置静态资源的部分。spring-doc.cadn.net.cn

ETagFilter

您可以使用ShallowEtagHeaderFilter添加“浅”eTag从 响应内容,从而节省带宽,但不能节省 CPU 时间。请参阅浅 ETagspring-doc.cadn.net.cn