📜  HTTP-缓存

📅  最后修改于: 2020-12-14 05:22:23             🧑  作者: Mango


HTTP通常用于分布式信息系统,其中可以通过使用响应缓存来提高性能。 HTTP / 1.1协议包含许多旨在使缓存起作用的元素。

HTTP / 1.1中的缓存目标是在许多情况下消除发送请求的需要,在许多其他情况下消除发送完整的响应的需要。

HTTP / 1.1中的基本缓存机制是指向服务器指定过期时间和验证器的缓存的隐式指令。为此,我们使用Cache-Control标头。

Cache-Control标头允许客户端或服务器在请求或响应中传输各种指令。这些指令通常会覆盖默认的缓存算法。缓存指令在逗号分隔的列表中指定。例如:

Cache-control: no-cache

客户端可以在其HTTP请求中使用以下缓存请求指令:

S.N. Cache Request Directive and Description
1 no-cache

A cache must not use the response to satisfy a subsequent request without successful revalidation with the origin server.

2 no-store

The cache should not store anything about the client request or server response.

3 max-age = seconds

Indicates that the client is willing to accept a response whose age is not greater than the specified time in seconds.

4 max-stale [ = seconds ]

Indicates that the client is willing to accept a response that has exceeded its expiration time. If seconds are given, it must not be expired by more than that time.

5 min-fresh = seconds

Indicates that the client is willing to accept a response whose freshness lifetime is not less than its current age plus the specified time in seconds.

6 no-transform

Does not convert the entity-body.

7 only-if-cached

Does not retrieve new data. The cache can send a document only if it is in the cache, and should not contact the origin-server to see if a newer copy exists.

服务器可以在其HTTP响应中使用以下缓存响应指令:

S.N. Cache Response Directive and Description
1 public

Indicates that the response may be cached by any cache.

2 private

Indicates that all or part of the response message is intended for a single user and must not be cached by a shared cache.

3 no-cache

A cache must not use the response to satisfy a subsequent request without successful re-validation with the origin server.

4 no-store

The cache should not store anything about the client request or server response.

5 no-transform

Does not convert the entity-body.

6 must-revalidate

The cache must verify the status of stale documents before using it and expired ones should not be used.

7 proxy-revalidate

The proxy-revalidate directive has the same meaning as the must- revalidate directive, except that it does not apply to non-shared user agent caches.

8 max-age = seconds

Indicates that the client is willing to accept a response whose age is not greater than the specified time in seconds.

9 s-maxage = seconds

The maximum age specified by this directive overrides the maximum age specified by either the max-age directive or the Expires header. The s-maxage directive is always ignored by a private cache.