当前位置:网站首页>HTTP cache - HTTP authoritative guide Chapter VII

HTTP cache - HTTP authoritative guide Chapter VII

2022-04-23 19:15:00 Li Siwei

WEB Caching can automatically save copies of common documents HTTP equipment .

  • Reduce redundant data transmission : Saves network costs . When there is no cache , Same request , The server generates a document every time .
    After there is a cache server , Generate documents only on the first request , And cache the document on the cache server , Subsequent requests are obtained directly from the cache server . The redundant transmission of network data of the original server is reduced .
  • Alleviate the network bottleneck problem : The request is partially distributed to the cache server , Original server
  • Instant congestion : When a large number of users access the original server at the same time , Cause instant congestion . And after caching , Only the first request requires access to the original service area , Subsequent requests can be provided by the cache server , Solve the congestion problem .
  • Distance delay : Requests can be routed to the nearest cache server , Instead of the original server far away , Reduce the network delay caused by geographical location .

hit 、 Not hit

If a copy of the requested resource already exists on the cache server , Cache hit .
If the requested resource does not have a copy on the cache server , Cache miss .

HTTP Revalidation 、 Freshness test

The cache should keep the copy the latest updated resource .

  • If-Modified-Since: The cache server will initiate a request for a resource at the client 、 And the copy is old enough to need to be verified again , A revalidation message will be sent to the original server , ... will be added to the message if-modified-since This Header, With GET Command send ,
  • 304 Reverify hit : If the server receives the revalidation message , It is found that the resource has not been updated compared to the cached copy , The response 304, It's called revalidation hit .304 In the response message , The data object is not obtained from the server , So it's a little faster than requesting the original server directly .
  • Re verify misses : The resource has been updated , The server will respond to a 200 OK Message of , And send the latest data object to the cache server .
  • 404 The object has been deleted : The resource has been deleted on the server , Then the server responds 404 To the cache server . The cache server is connected to 404 Delete its copy .

cache hit rate 40% reasonable

The proportion of the number of requests responded by the cache to the total number of requests .
Improving the cache hit rate is beneficial to reducing the network delay .

Byte hit rate

Due to different sizes of requested resources , Some size objects may be accessed less often , But due to size , It contributes more to the whole data flow
Improving the byte hit rate is beneficial to saving bandwidth .

How does the client distinguish whether the response is from the cache or the original server

  • via: Some commercial agents will be in via Add some additional information to , To describe the cache hit
  • Date: The client can send the... In the response header Date Compare with the current time , If Date The time is much earlier than the current time , You can think of it as a cache .
  • Age:

Freshness test

  1. How to confirm whether freshness test is needed ?

cache-control : maxAge=1234455
Date : 2019 01 01 23:59:59

Date Is the first request , The creation time of the response message generated by the server ,maxAge Is the survival time of the copy .
If the current time is greater than Date+maxAge, Then the copy has expired , Freshness test is required

Expires : 2019 03 01 23:59:59
If the current time is greater than Expires, Then the copy has expired , Freshness test is required

  1. How to test freshness ?

last-modified: 2019 03 01 23:59:59 # HTTP Response head
if-modified-since : 2019 03 01 23:59:59 # HTTP Request header

The cache server will HTTP Respond to... In the copy last-modified The value of is used as the... In the request message if-modified-since value .
Send to the original server with if-modified-since Of header Message of , The server matches this time with the time of the latest resource last-modified Comparison .
if-modified-since The value of should be taken as... In the original copy last-modified Value .

Etag:“v4.6”,“v4.7” # Http Response head , The original server sends... To the cache
If-none-Match:“v4.6”,“v4.7” # http Request header , The cache sends a message to the original server

The cache server will the response headers in the replica Etag As a condition , Send to

  1. How the original server responds to the freshness detection request of the cache server ?
    304 not modified: If the cache content is still up to date , Then... Will not be sent in the response message body data , However, the cache server will update the response header in the copy according to the response header , Include last-modified、Date、cache-control:maxAge、Expires、Etag etc.
    200 OK: If the copy of the cache server has expired , Then the original server will respond the new resource content to the cache . The cache server will take the response message as a new copy , Replace the original copy .
    404: If the original server has deleted this resource , Then the cache will also delete the resource copy .

  2. Cache-control Meaning in response header
    no-store: The original server tells the cache server , This response message should not be copied
    no-cache: The original server tells the cache server , This response message can be cached , But every time a client requests this resource , Freshness verification is required
    must-realidate:
    max-age=123456: s-maxage=123456( Only for shared cache )
    Expires:

  3. cache-control Meaning in request header
    no-store: The client tells the cache , All documents for this resource should be deleted from the cache as soon as possible , Because it may contain sensitive information . Tell cache , Do not cache a copy of this resource
    no-cache: The client tells the cache server , The freshness of resources must be verified , Then respond with the latest copy
    max-age=123445: The client tells the cache , It is not allowed to return data that has been cached for more than max-age Copy of , If the cache time of the copy has exceeded the given max-age, Get the latest copy from the original server , Re response
    min-fresh=12333: The client tells the cache , If the expiration time from the copy does not exceed min-fresh, You must get the latest copy from the original server , Respond to the latest copy to the client
    only-if-cache: The client tells the cache , Only if a copy exists in the cache , The client will get a copy

版权声明
本文为[Li Siwei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210600381066.html