HTTP caching and mandatory consultation cache

Web Cache can reduce network latency and blocking, thus reducing the display time of a resource being used. With HTTP caching, Web sites become more responsive.

Cache advantages:

  • Reduce unnecessary data transmission, to conserve bandwidth
  • Reduce the burden on the server, improve site performance
  • Speed ​​up the client loads pages, user-friendly experience

Cache Disadvantages:

  • After the server resource updates, client updates lag

Browser cache there are two categories:

  1. Forced Cache
  2. Cache consultation

Forced Cache

Does not send a request to the server, the resource is read directly from the cache, a request to return a status code 200;

Forced cache, the server may cache-control Response Headers in the cache a cache is defined like manner, as shown below:



or:

cache-control field

cache-control: max-age=xxxx,public

  • The client and the proxy server can cache the resource;
  • The client in xxx seconds period, if there is demand for the resource request, then directly read cache, statu code: 200
  • If you do a refresh operation, initiates http request to the server

cache-control: max-age=xxxx,private

  • Only allows clients to cache the resource; do not allow caching proxy server
  • The client reads the cache directly xxx seconds, statu code: 200

cache-control: max-age=xxxx,immutable

  • The client in xxx seconds period, if there is demand for the resource request, then directly read cache, statu code: 200
  • Even if the user did a refresh operation, do not initiate http request to the server

cache-control: no-cache

  • Skip strong cache settings, but without prejudice to negotiate cache settings; if you do generally strong cache, a cache miss only strong and we have to go consultations cache, set the no-cache cache will not be strong, and every request back ask the server.

cache-control: no-store

  • Do not cache, this will let the client, not the server cache, there is no so-called strong cache, the cache negotiation.

Added: cache there is a mandatory field in the header can be set expiration time, that Expires, but something Expires HTTP 1.0 is now the default browser are using the default HTTP 1.1, so its role largely ignored.

Cache consultation

Sends a request to the server, the server in accordance with some parameters of this request Request Headers (ETag and last-modified) negotiation to determine whether a cache hit, if hit, the status code 304 is returned, and bring new Request Headers notifies the browser from read cache resource;

Cache consultation mainly in the Response Headers in etag and last-modified:

etag

  • That file hash, each unique file

last-modified

  • File modification time, accurate to the second

note:

Response Headers in etag, last-modified again when the client initiates a request to the server, it may change the key name in the Request Headers: if-none-matched and if-modified-since

// Response Headers
etag: 65597c1615681857158408944e
last-modified: Wed, 11 Sep 2019 06:20:13 GMT

// Request Headers 变为
if-none-matched: 65597c1615681857158408944e
if-modified-since: Wed, 11 Sep 2019 06:20:13 GMT

Two kinds of caches in common: both read the resource from the client cache; the difference is strong cache does not send a request to the server, the cache will send a request consultation



Reference: https://www.jianshu.com/p/9c95db596df5

Guess you like

Origin www.cnblogs.com/cckui/p/11506514.html