HTTP caching (web caching)

Note: Common HTTP caches can only store GET responses

Website and application performance can be significantly improved by reusing previously fetched resources
Web caching reduces request latency and saves network traffic

Caching can basically be divided into two broad categories

  • Private cache (browser cache)
  • Shared cache (proxy cache)

private cache

Private cache refers to a cache that can only be used by a single user.
For example, pages opened through a browser will be saved here in the browser, providing functions such as backward/forward navigation, saving web pages, and viewing source code.

shared cache

A shared cache is a cache that is accessible to all users.
We often use nginx as a reverse proxy. Here we assume that the target server has a picture of 1.jpg, and we have set up a redis cache server.
When 10,000 users request this picture, nginx has to get 1.jpg from the target server every time. .jpg and then returned to the client.
However, assuming that 1.jpg will not change for 10,000 years, so requesting the same resource every time puts pressure on the target server.
Therefore, when the resource is requested first, the cache server After 1.jpg can be cached,
all nginx requests can go directly to the cache server to get this resource, which greatly reduces the pressure on the server

Cache-control

Caching is conditional, we must control how to cache according to a certain strategy

The Cache-Control header indicates how to handle the cache,
both request and response headers support this property. Define the caching strategy through the different values ​​it provides.

Many people confuse with no-storeand no-cachethink that no-cachethere is no cache, they are different

  1. no cache
    Cache-Control: no-store
    不得存储任何关于客户端请求和服务端响应的内容,每次请求都是最新资源

  2. Cache but requires authentication when used ( not not cached )
    Cache-Control: no-cache
    请求发出时,缓存服务器会将此请求发到服务器验证请求中所描述的缓存是否过期,若未过期,则缓存服务器才使用缓存(也就是我们常见的 304)

  3. private cache
    Cache-control: private
    表示这些缓存专用于某个用户的,缓存服务器不能缓存此响应,也就是交给浏览器控制这些缓存

  4. shared cache
    Cache-control: public
    表示这些缓存可以被缓存服务器缓存,也就是设置了 public 你才能缓存上边说的 1.jpg

  5. expired cache
    Cache-Control: max-age=100
    表示资源能够被缓存的最大时间(秒),超过这个时间表示资源过期,注意该字段的优先级大于 Expires。如果设置了max-age,会忽略 Expires 头

  6. self validating cache
    Cache-Control: must-revalidate
    使用缓存资源时,必须先验证它的状态,已过期的缓存将不被使用

Here I will give an answer to the doubts I had when I was studying.

  1. How does Cache-control: max-age=100 work?
    When the client sends a request to the cache server, first check whether there is a cache
    or not. The cache server will send the request to the target server. If a response is received { Cache-contol: “max-age=100” } the
    cache server will use The storage is returned to the client, and the response header { Cache-control: “max-age=100”, Age: 0 }
    10s later, the client sends the request to the cache server again.
    After the cache server detects the cache, it returns the resource and adds the response header to the response Header { Cache-control: “max-age=100”, Age: 10 }
    After 100s, the client sends the request to the cache server again.
    The cache server detects that the cache has expired, and then sends the request to the target server with the request header, If-None-Match: ...
    If the target server returns 304, the cache server will return the resource to the client, and if the response header { Cache-control: "max-age=100", Age: 0 }
    returns 200, then Cache the latest resource and return it to the client
    insert image description here

  2. no-cacheAnd must-revalidatewhat's the difference?
    no-cache Each time the server is asked whether the resource expires. Both the request header and the response header may have
    must-revalidatea judgment expiration time. If it has not expired, the resource will be returned directly, otherwise the request will be re-requested. It is only used for response headers
    Cache-control: no-cacheand Cache-control: max-age=0, must-revalidatehas the same effect as

  3. ExpiresWhat Cache-control: max-age=100's the difference
    Expires ? The priority is lower than that max-age. Expires indicates the date of expiration, indicating that an absolute time (year/month/day: minute: second) expires. The priority is higher than that,
    max-ageindicating Expiresthe expiration time (seconds), indicating a certain time ( 100s) after expiration

  4. Cache header priority
    Cache: max-age=x >>ExpiresLast-Modified

  5. What does the cache server use to request the server to determine whether the cache is expired?
    When the resource is expired, the cache server will If-None-Matchsend the request plus the field to the target server. If the server returns 304, the cache server will update the cache resource information and return the resource.
    If-None-MatchThe value of the response header Etagis Etaga string of identifiers generated according to the resource. If the resource has not changed, it will not change

  6. How does Last-Modified calculate the expiration time of the cache?
    The value of Date in the response header minus the value of Last-Modified is divided by 10. As for why it is divided by 10, this is stipulated in the agreement, and I don’t know

  7. The cache expiration date and time calculation formula
    expirationTime = responseTime + freshnessLifetime - currentAge
    responseTimeindicates the time point when the response is received,
    freshnessLifetimeindicates the value of max-age, or the value of Data - Expires, or the expiration time of Last-Modified above
    currentAgeindicates the time of this response The number of seconds elapsed at that point in time, that is, the Age in the response header

Summarize the strong cache and negotiation cache that I understand. The difference between strong cache and negotiated cache is whether there is no-cache, one is to directly hit the local cache, and the other is to verify with the server whether the cache can be used

My ability is limited, so if there is anything wrong, please point out

Guess you like

Origin blog.csdn.net/weixin_42335036/article/details/124245831