One of the cache series: buffer, cache, and browser caching
One: The cache is to adjust the speed of the two or more different velocity inconsistent speed, in the middle of the faster side of a slow speed of access to a party, such as CPU level, level two cache is to save the CPU's most frequently accessed data, memory is to save the CPU frequently access to the hard disk data, And the hard disk also has the size of the cache, even the physical server Raid card has also cached, all in order to speed up the CPU to access the hard disk data, because the CPU speed is too fast, the CPU needs the data hard disk often can not meet the needs of the CPU in a short time, so the PCU cache, memory, Raid Cards and hard disk caches to some extent meet the CPU's data requirements, that is, the CPU reads data from the cache can greatly improve the efficiency of the CPU.
1.1: Understanding Buffer and cache:
Buffer: Buffering is also called write buffer, generally used for write operations, the data can be written into the disk, buffer is generally used for write buffer, used to solve different media speed inconsistent buffering, the data is temporarily written to the nearest place, to improve the write speed.
The CPU writes the data line to the memory disk buffer, then thinks that the data has been written, and then the kernel thread writes to disk at a later time, so the server suddenly loses some of the data in memory.
Cache: Caching is also called Read cache, commonly used for read operations, CPU read files from memory read, if the memory is not first read from the hard disk memory and then read to the CPU, will need to frequently read the data in its own recent cache area, the next time can be read quickly.
Save location for 1.2:cache:
Client: Browser
Memory: local server, remote server
Hard disk: Local hard disk, remote server hard disk
Speed comparison:
client browser-memory-remote memory-hard disk-remote hard disk
Features of the 1.3:cache:
1. Expiry time
2. Forced expiration, the CDN is not updated after the source website updates the picture, need to force is the picture cache expires
3. Hit rate, which is the read hit rate of the cache
1.4: Save the memory to the hard disk, and then empty the memory, there may be some memory data is the process dead can not be freed:
#sync # Quickly writes data from the buffer to the hard disk
# cat/proc/sys/vm/drop_caches
0
#1是用来清空最近放问过的文件页面缓存
#2是用来清空文件节点缓存和目录项缓存
#3是用来清空1和2所有内容的缓存.
Two: User Layer cache:
2.1:dns Cache:
2.1.1: The default is 60 seconds, that is, within 60 seconds to access the same domain name is not DNS resolution:
To view the DNS cache for your Chrome browser:
chrome://net-internals/#dns
2.1.2:dns pre-fetch, only in HTML 5 support, when a page contains multiple domain names when the browser will first try to resolve the domain name and cache, and then use the time can be directly only do not need to DNS resolution:
2.2: Browser cache:
2.2.1: Firefox Cache location:
2.2.2: File access time, last modified time, and change time:
#Statindex.php File: ' index.php ' Size:4311Blocks: -IO Block:4096Regularfiledevice:fd01h/64769d Inode:68497957Links:1Access: (0755/-rwxr-xr-x) Uid: (497/Nginx) Gid: (497/nginx) Access: .- -- + Geneva:xx:03.046492759+0800# Last Read time modify: .-Geneva- - -: +:47.000000000+0800# The time when the content of the block changed when the last change occurred: .- on- One the: -:35.872038074+0800# permissions, etc. change the time the inode sends changes
2.2.3: The last modification time of the cache negotiation mechanism: The system call gets the last modification time of the file and returns to the browser 304 status code if it does not change, indicating that there is no change, and then the browser uses the local cache to display the resource
2.2.4: Cache negotiated ETag:
The browser contains an etag tag in the returned message, and then the browser saves the ETag, which is judged when the ETag is received again, using the local cache if the ETag has not changed
2.2.5: Cache negotiation Expiration time, both need to send a request, that is, regardless of whether the resource is out of date to send a request for negotiation, which will consume unnecessary time, so there is a cache expiration time, that is, the first time the resource is requested with a resource expiration time, the default is 30 days, This method is used more than the table, but there is no guarantee that the customer's time is accurate and consistent, so if a maximum life cycle, using the user's local time to calculate the cache data is more than how many days, the following expiration time is 2017, However, the maximum lifetime of the cache is calculated as day equals 691,200 seconds, or 2 days, and the expiration time is as follows:
Maximum lifetime, in seconds, based on the client's time to calculate whether the cache days are exceeded
Three: About refresh:
3.1: Press ENTER directly in the browser input address bar:
All non-expiring data in the browser uses the local cache directly, that is, data that does not expire does not send a cached negotiated request.
3.2: Press F5 or the refresh button on the browser:
The browser will attach the cache negotiation message on the request header, the browser cannot use the local cache directly, it needs to be validated before it can be used, but the expiration time does not exceed the date of the cache is unaffected, is affected by the last modification time and ETag.
3.3:ctrl+f5 or press CTRL + Refresh, this is a forced flush, the browser will send all requests to the server and will not use the local cache
3.4: How to get the latest server data resources for the client:
3.4.1: Change the name of a resource so that it is cumbersome and error-prone every time it is changed
3.4.2: Add timestamp, new version of new timestamp, browser discovery time is newer will get new resources, such as xxx.js?20170724 than xxx.js?20170101 new
3.4.3: Static resources use CDN to add parameters, such as URL can take parameters, with parameters, CND will hash the URL, when the source URL resource send changes (deliberately change the parameters, because the static resources updated, requires CND also updated) CDN found that the hash value is not the same will be updated, in addition to the CDN can be determined by the suffix name plus parameters, that is, some suffixes have parameters some suffixes have no parameters
One of the cache series: buffer, cache, and browser caching