📜  HTTP缓存

📅  最后修改于: 2021-01-03 03:09:39             🧑  作者: Mango

HTTP缓存

HTTP用于分布式操作系统,在其中使用响应缓存,我们可以提高性能。 HTTP / 1.1协议包含许多元素,这些元素旨在使缓存正常工作。因为这些元素相互交互,并且它们是协议其他方面不可分割的,所以将HTTP缓存设计与响应代码,方法,标头等的详细描述分开描述是很有用的。

在许多情况下,HTTP缓存基本上用于消除发送请求的需要。在许多其他情况下,它也用于消除发送完整响应的需要。

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

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

Cache-control: no-cache

客户端在其HTTP请求中使用缓存请求指令,如下所示:

S.N. Directive of Cache Request and Description
1 no-cache
It indicates that the response which is returned and cannot be used to satisfy a subsequent request to the same URL.
2 no-store
It indicates that the client will not store anything about the request of the client or the response of the server.
3 max-age = seconds
It is used to indicate that the client is willing to accept a response whose age is not greater than the time which is specified in seconds.
4 max-state [=seconds ]
It is used to indicate that the client is willing to accept a response that has exceeded its expiration time. It must not be expired by more than that time, if the seconds are given.
5 min-fresh= seconds
It is used to indicate that the client is willing to accept a response whose lifetime of freshness is not less than its current age plus, which is the specified time in seconds.
6 no-transform
It does not convert the entity-body.
7 only-if-cached
It does not retrieve new data. A document can be send by the cache only if it is in the cache and should not contact the origin-server to see if it exists a newer copy.

服务器可以在其HTTP响应中使用高速缓存响应的指令,如下所示:

S.N. Directive of Cache Response and Description
1 public
It indicates that any cache may be cached the response.
2 private
It is used to indicate that some part of the response message or all the response message is intended for a single user. It must not be cached by a shared cache.
3 no-cache
It indicates that the response which is returned cannot be used to satisfy a subsequent request to the same URL.
4 no-store
It indicates that the client will not store anything about the request of the client or the response of the server.
5 no-transform
It does not convert the entity-body.
6 must-revalidate
It indicates that the cache must verify the stale document’s status before using it, and expired ones should not be used.
7 proxy-revalidate
The proxy-revalidate directives and the must-revalidate directive have the same meaning, except that it does not apply to non-shared user agent caches.
8 max-age = seconds
It is used to indicate that the client is willing to accept a response whose age is not greater than the time which is specified in seconds.
9 s-maxage = seconds
The maximum age specified by the directives overrides the maximum age specified by either the Expires header or the max-age directives. The private cache always ignores the s-manage directives.