1. Cache
The corresponding engineers are too familiar with caching, which is too convenient. the way resources or data are obtained is omitted, and the cache is directly the fastest place for user access, which also reduces the pressure on the server. For example:
(1) static file acquisition
Server> CDN> Local disk> Local Memory
(2) Data Acquisition
Databases> memory databases
(3) host Cache
Master Domain Name Server-> top-level domain name server-> Root Domain Name Server-> network service provider cache-> router cache-> system cache-> browser cache
The benefits of caching are self-evident. It increases the access speed of users and reduces the pressure on the server. The problem is that multiple copies of data are stored in different places, and the cache data consistency is critical. This article mainly talks about the most common browser cache in the host cache.
2. Talk about browser cache
Glossary:
(1) differences between expires and cache-control:
Same: it is used to determine the expiration time of the local cache of the resource.
Expires: absolute time. If the server and local time are inconsistent, problems may occur.
Cache-control: relative time, better compatibility
2.1 browser cache Classification
Browser cache is divided into strong cache and negotiated cache, matching the cache process:
(1) Before sending a request, the browser determines whether to use a strong cache policy based on the expires and cache-control of the Request Header. If a request is hit, the browser directly retrieves resources from the cache and does not send a request, response Code: 200 (from memory cache or from disk cache). If no hit occurs, proceed to the next step.
(2) the browser will send a request based on the last-modified or etag in the request header to determine whether the request hits the negotiated cache. If the request hits the cache, the server will not return the resource, instead, an httpcode (304) is returned. The browser loads the file from the local cache based on the httpcode.
(3) If no hit is found in the first two steps, the server directly obtains the resource and caches it locally.
2.2 strong cache 2.2.1 strong cache principle:
Strong cache requires the server to set expires and cache-control.
(1) Add cache-control to the respone header when the server requests resources from the server for the first time and returns resources.
(2) After receiving the resource, the resource will be cached together with all response headers (the headers returned by the cache hit requests are from the cached headers)
(3) When you access this resource again, find it from the cache. If the request time is before the cache-control set in the cache, load the file directly from the cache.
(4) If the cache is not hit, when the browser loads resources directly from the server, the cache-control will be updated during reload.
2.2.2 high cache Classification
(1) From disk cache: the cache is cached on the hard disk, the browser is closed, and the cache is still
(2) From memory cache: the cache is in the memory, the browser is closed, and the cache is cleared.
2.3 negotiate Cache
The browser sends a request to the server to verify whether it hits the negotiated cache. if it hits the cache, the service will not return the file and return httpcode (304 not modified ), the browser loads files from the local cache
2.3.1 negotiate Cache Management
The negotiated cache uses the headers of [last-modified, if-modified-since] and [etag, if-None-match] for management.
(1) After sending the request Server File, the last-modified is added to the response header, indicating the latest modification time.
(2) When you resend the request to obtain this file, the request header carries if-modified-since, indicating that the last request was cached in the response header of the local file.
(3) After receiving the request, the server returns httpcode (304 not modified ), the last-modified header will not be added to the response header, which indicates that it hits the negotiated cache.
(4) If the negotiation cache is not hit, when the browser loads resources directly from the server, the last-modified header will be updated during reload. The next request, if-modified-since changes to the last returned last-modified value.
Improvement: the content of the server file has been modified, but the modification time has not changed. Therefore, etag is generated. A string is generated based on the file content. If the file content changes, etag is changed, it no longer depends on the modification time.
3. Host cache problems and causes
1. When the website is changed, the cached IP address and domain name are not changed in time, leading to Website access failure.
Reason: the DNS Cache has a certain expiration time. If the DNS caches the real IP address and domain name ing of your server, if the server is unavailable, the host cache link shown above, basically, the cache will not expire, and access will fail.
Resolve: to directly cache domain names and servers, you must ensure that the server is always available; otherwise, some users will not be able to use it. Therefore, we generally bind the domain name to the proxy server, the proxy server is only responsible for accepting, forwarding requests, and load balancing, and is not responsible for application release and upgrade. Therefore, the proxy server is almost invisible, which greatly solves the problem of inaccessible DNS cache.
2. After modifying the host, the browser still accesses the IP address.
Reason: This is generally the browser cache or system cache.
Resolve: Clear the browser cache or system cache.
3. modified the host. Some resources are old and some are new.
Reason: many new browsers enable the memory cache, which caches some data or files to the local memory. However, if the host is modified, the cached data is accessed.
Resolve: clears the browser memory and memory cache.
4. One trick to clear the browser cache 4.1 Chrome browser
(1) Access: chrome: // net-internals/# DNS
(2) Click the inverted triangle in the upper right corner: Clear cache to clear the domain name and IP address cache.
(3) Click the inverted triangle in the upper right corner: flush sockets to close the opened sockets.
(4) access the domain name again to access the correct IP address and resources.
How to disable browser cache permanently:
(1) Open the browser and "check"
(2) in the upper-right corner of the check box, select more tools
(3) Select network condition
(4) Select disable Cache
4.2 Safari
Disable browser cache:
Principle: when a request is sent, the request header carries no-Cache
Host cache and browser cache --- solve the damage caused by host Cache