📜  RESTful Web服务-缓存

📅  最后修改于: 2020-10-20 04:38:50             🧑  作者: Mango


缓存是指将服务器响应存储在客户端本身中,因此客户端无需一次又一次地请求服务器请求相同的资源。服务器响应应该具有有关如何进行缓存的信息,以便客户端在一段时间内缓存响应,或者永远不要缓存服务器响应。

以下是服务器响应可以具有的标头,用于配置客户端的缓存-

Sr.No. Header & Description

1

Date

Date and Time of the resource when it was created.

2

Last Modified

Date and Time of the resource when it was last modified.

3

Cache-Control

Primary header to control caching.

4

Expires

Expiration date and time of caching.

5

Age

Duration in seconds from when resource was fetched from the server.

缓存控制标头

以下是Cache-Control标头的详细信息-

Sr.No. Directive & Description

1

Public

Indicates that resource is cacheable by any component.

2

Private

Indicates that resource is cacheable only by the client and the server, no intermediary can cache the resource.

3

no-cache/no-store

Indicates that a resource is not cacheable.

4

max-age

Indicates the caching is valid up to max-age in seconds. After this, client has to make another request.

5

must-revalidate

Indication to server to revalidate resource if max-age has passed.

最佳实践

  • 始终保持静态内容(如图像,CSS,JavaScript)可缓存,并且有效期为2到3天。

  • 切勿将有效期过高。

  • 动态内容应仅缓存几个小时。

缓存控制的最佳做法