Git Product home page Git Product logo

Comments (4)

vivipure avatar vivipure commented on August 24, 2024

缓存

强缓存

浏览器从本地缓存中获取数据,不需要与服务器交互。状态码200

Header 中的 Expires 和 Cache-Control 控制强缓存

Cache-Control:

  1. no-cache 不使用本地缓存。需要使用协商缓存
  2. no-store 禁止浏览器缓存数据,每次都会重新请求
  3. public 允许所缓存,包括用户,cdn等中间服务器
  4. private: 只能被浏览器缓存,不允许中间服务器缓存。优先级大于Expires
  5. max-age=1200. 表示资源的有效期。过期后就会重新请求

Expires:

expires: Fri, 14 Apr ..

表示缓存的具体过期时间。超过这个时间后就会重新请求。由于这个时间是服务端计算的。如果服务端于客户端的时间存在偏差时,该功能可能会存在问题

协商缓存

如果不走强缓存,就会走协商缓存。让服务器来确定缓存的资源是否可用

Etag

返回资源的唯一标识,当资源内容改动后,Etag会变化。

If-None-Match

浏览器再次请求同一资源时,会发送If-None-Match,值为缓存的Etag值。

如果值和服务端相同,则返回 If-None-Match 为 false,返回状态码 304.客户端继续使用本地缓存

不同返回 If-None-Match 为 true, 返回新的数据,状态码为200.客户端使用新返回的数据

Last-Modified

资源在服务器最后修改时间. 该功能相较于Etag 存在一些问题:

  1. 可能文件修改时间改了,内容没有变化
  2. 时间精确到秒级,但文件改动如果时毫秒级变动,数据不准确

因此 Last-Modified 优先级低于Etag

If-Modified-Since

功能和 If-None-Match 类似。客户端再次请求时,会发送至之前返回的 Last-Modified 时间。服务端会与文件最后修改时间进行判断。

如果时间一致,则返回 304, 本地使用缓存

如果时间不一致,则返回新内容。 返回状态码 200

from til.

vivipure avatar vivipure commented on August 24, 2024

HTTP连接过程和HTTPS连接过程

WIP

from til.

vivipure avatar vivipure commented on August 24, 2024

常见的状态码

WIP

from til.

vivipure avatar vivipure commented on August 24, 2024

HTTP 连接效率

HTTP 1.0

在 HTTP 1.0 时,每次链接都会建立一个TCP连接,连接成本大。浏览器限制同个域名的请求最多并发6个(Chrome),所以超出的请求只能在等待队列中进行排队。

HTTP 1.1

在HTTP 1.1 时,引入了 Keep-Alive, 同一个域名的请求共用一个TCP连接,多个连接不会重新创建TCP链接。但是还是受限于最大并发数的限制,性能上相较于HTTP 1.0 有提升。

HTTP 1.1 还引入了 pipe, 用来解决最大并发数。 pipe允许将所有请求都发给服务器,服务器按顺序一个个响应。节省了发送的等待时间,但是如果有一个请求阻塞了,会导致后面的请求都会被阻塞。

优化HTTP 1.1 的请求速度

  1. 请求分散,使用不同域名访问资源,解决浏览器同个域名最多并发数的限制
  2. 请求合并,例如雪碧图

HTTP 2

来到HTTP 2.0 时,由于HTTP 2.0基于二进制帧,不是基于文本的。可以做到无序传输。所有的请求都是建立在一个HTTP连接上的,由于所有数据都是流,不会存在互相阻塞的问题。实现了多路复用的功能。但是还是无法解决TCP队头阻塞的问题。

from til.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.