Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
ef41dcf6
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ef41dcf6
编写于
6月 11, 2018
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add HTTP caching to WebFlux section
Issue: SPR-16395
上级
4a435c12
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
122 addition
and
0 deletion
+122
-0
src/docs/asciidoc/web/webflux.adoc
src/docs/asciidoc/web/webflux.adoc
+118
-0
src/docs/asciidoc/web/webmvc.adoc
src/docs/asciidoc/web/webmvc.adoc
+4
-0
未找到文件。
src/docs/asciidoc/web/webflux.adoc
浏览文件 @
ef41dcf6
...
...
@@ -2540,6 +2540,124 @@ include::webflux-view.adoc[leveloffset=+1]
[[webflux-caching]]
== HTTP Caching
[.small]#<<web.adoc#mvc-caching,Same in Spring MVC>>#
HTTP caching can significantly improve the performance of a web application. HTTP caching
revolves around the "Cache-Control" response header and subsequently conditional request
headers such as "Last-Modified" and "ETag". "Cache-Control" advises private (e.g. browser)
and public (e.g. proxy) caches how to cache and re-use responses. An "ETag" header is used
to make a conditional request that may result in a 304 (NOT_MODIFIED) without a body,
if the content has not changed. "ETag" can be seen as a more sophisticated successor to
the `Last-Modified` header.
This section describes HTTP caching related options available in Spring Web MVC.
[[webflux-caching-cachecontrol]]
=== `CacheControl`
[.small]#<<web.adoc#mvc-caching-cachecontrol,Same in Spring MVC>>#
{api-spring-framework}/http/CacheControl.html[`CacheControl`] provides support for
configuring settings related to the "Cache-Control" header and is accepted as an argument
in a number of places:
* <<webflux-caching-etag-lastmodified>>
* <<webflux-caching-static-resources>>
While https://tools.ietf.org/html/rfc7234#section-5.2.2[RFC 7234] describes all possible
directives for the "Cache-Control" response header, the `CacheControl` type takes a
use case oriented approach focusing on the common scenarios:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
// 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();
----
[[webflux-caching-etag-lastmodified]]
=== Controllers
[.small]#<<web.adoc#mvc-caching-etag-lastmodified,Same in Spring MVC>>#
Controllers can add explicit support for HTTP caching. This is recommended since the
lastModified or ETag value for a resource needs to be calculated before it can be compared
against conditional request headers. A controller can add an ETag and "Cache-Control"
settings to a `ResponseEntity`:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@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);
}
----
This will send an 304 (NOT_MODIFIED) response with an empty body, if the comparison
to the conditional request headers indicates the content has not changed. Otherwise the
"ETag" and "Cache-Control" headers will be added to the response.
The check against conditional request headers can also be made in the controller:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RequestMapping
public String myHandleMethod(ServerWebExchange exchange, Model model) {
long eTag = ... <1>
if (exchange.checkNotModified(eTag)) {
return null; <2>
}
model.addAttribute(...); <3>
return "myViewName";
}
----
<1> Application-specific calculation.
<2> Response has been set to 304 (NOT_MODIFIED), no further processing.
<3> Continue with request processing.
There are 3 variants for checking conditional requests against eTag values, lastModified
values, or both. For conditional "GET" and "HEAD" requests, the response may be set to
304 (NOT_MODIFIED). For conditional "POST", "PUT", and "DELETE", the response would be set
to 409 (PRECONDITION_FAILED) instead to prevent concurrent modification.
[[webflux-caching-static-resources]]
=== Static resources
[.small]#<<web.adoc#mvc-caching-static-resources,Same in Spring MVC>>#
Static resources should be served with a "Cache-Control" and conditional response headers
for optimal performance. See section on configuring <<webflux-config-static-resources>>.
[[webflux-config]]
== WebFlux Config
[.small]#<<web.adoc#mvc-config,Same in Spring MVC>>#
...
...
src/docs/asciidoc/web/webmvc.adoc
浏览文件 @
ef41dcf6
...
...
@@ -3708,6 +3708,7 @@ http://hdiv.org/[HDIV] is another web security framework that integrates with Sp
[[mvc-caching]]
== HTTP Caching
[.small]#
<
<
web-reactive.adoc#webflux-caching
,
Same
in
Spring
WebFlux
>
>#
HTTP caching can significantly improve the performance of a web application. HTTP caching
revolves around the "Cache-Control" response header and subsequently conditional request
...
...
@@ -3723,6 +3724,7 @@ This section describes HTTP caching related options available in Spring Web MVC.
[[mvc-caching-cachecontrol]]
=== `CacheControl`
[.small]#
<
<
web-reactive.adoc#webflux-caching-cachecontrol
,
Same
in
Spring
WebFlux
>
>#
{api-spring-framework}/http/CacheControl.html[`CacheControl`] provides support for
configuring settings related to the "Cache-Control" header and is accepted as an argument
...
...
@@ -3764,6 +3766,7 @@ works as follows:
[[mvc-caching-etag-lastmodified]]
=== Controllers
[.small]#
<
<
web-reactive.adoc#webflux-caching-etag-lastmodified
,
Same
in
Spring
WebFlux
>
>#
Controllers can add explicit support for HTTP caching. This is recommended since the
lastModified or ETag value for a resource needs to be calculated before it can be compared
...
...
@@ -3823,6 +3826,7 @@ to 409 (PRECONDITION_FAILED) instead to prevent concurrent modification.
[[mvc-caching-static-resources]]
=== Static resources
[.small]#
<
<
web-reactive.adoc#webflux-caching-static-resources
,
Same
in
Spring
WebFlux
>
>#
Static resources should be served with a "Cache-Control" and conditional response headers
for optimal performance. See section on configuring
<
<
mvc-config-static-resources
>
>.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录