# # # About Cache
-What is a cache
Caching this thing is really ubiquitous, there are browser-side caches, server-side caches, proxy server caches, ASP. NET page cache, object cache. The database also has caches, and so on. The caching feature in HTTP is the browser cache, as well as the cache proxy server. HTTP caching refers to: When a Web request arrives at the cache, if there is a "cached" copy locally, the document can be extracted from the local storage device instead of from the original server
-Why Use Caching
1. The cache reduces redundant data transfer, not only reduces the cost of the traffic, but also saves the user time.
2. Reduce the burden of the server, the effective cache can not need to resend the server, greatly improving the performance of the site.
3. Alleviate the problem of network bandwidth, can load the page faster without needing more bandwidth.
4. Reduce the distance delay, because in the propagation of information, the longer the distance delay, the cache may exist on the local or near the (proxy) server, do not need a further distance to request.
-Types of caches
1. Private cache
A dedicated cache, called a private cache, does not require a lot of power or storage space, so it can be done in small, inexpensive way. We have private memory in our web browsers, most of which are in our PC's personal disk and in memory, and allow for setting size and so on. * * In the browser address input box and the direct F5 refresh takes a different mechanism, the former will see if there is a valid cache, the effective cache will organize the request flow to the original server, the latter will directly ask the original server, verify Notconsistent * *
2. Public cache
The public cache is a special shared proxy server that receives access from multiple users, the proxy cache provides the document from the local cache, and only takes one time for the popular object cache, and it uses the shared copy to serve all requests to reduce network traffic.
Process steps for # # # Cache
1. Receiving
Cache read incoming Request messages from the network
2. Parsing
The cache parses the message, extracting the URL and various headers.
3. Find
The cache checks to see if a local copy is available and, if not, gets a copy (and saves it locally).
The cache cannot save every document in the world and becomes a cache hit when it can serve certain requests that reach the cache with an existing replica, thus deriving the concepts of cache hit rate and byte hit ratio. Some other requests that reach the cache may be forwarded to the original server because no replicas are available, which is known as misses.
* Re-Verify *: The content of the original server is likely to change (the following freshness detection will be explained in detail) The cache is detected at any time, and this process does not require the entire object from the server to be quickly detected. In the development of the project it is also possible that our original changes could not be rendered because of the caching problem.
4. Freshness Detection
Cache to see if the cached copy is fresh enough, and if not, ask the server if there are any updates. (The header itself can enforce cached authentication)
-Document expires with a special HTTP Cache-control header and Expires header to mark the document's "shelf life".
-Server re-authentication
Just because the cached file is out of date does not mean that it is actually different from the document that is currently active on the original server, but only when it is time to audit, indicating whether the cache needs to access the original server or not. If a change occurs, the cache acquires a copy of the new document and saves it to the location of the old document, and if it does not change, the cache only needs to get a new header, including a new expiration date, and update the header in the cache.
-Validation with conditional methods
1. If-modified-since:data
> Specify a date or time to execute the request method if the document has been modified since the specified date. It can be used in conjunction with the Last-modofied server response header, which only gets content when it has been modified and is not the same as the cached version (note: This is not necessarily the later but a different one). If "Expired" is usually performed successfully, the document carrying the new header is returned to the cache, and if "not expired" returns only a new expiration date.
2. If-none-match: Entity label re-verification
Unlike above, this approach is to identify the version with an additional tag (any label or reference string attached to the document), because in some cases it is not sufficient to use the method above, for example: Some documents are periodically modified but contain the same data. Some data may have been modified, but this modification is not important, and some servers are unable to obtain the modification time.
3. Weak authenticator
5. Create a response
The cache constructs a response message with a new header and a cached theme.
6. Send
The cache sends the response back to the client over the network
7. Log
The cache optionally creates a log file entry to describe the transaction.
# # # Set Cache control
Different Web servers provide different mechanisms for setting HTTP Cache-control and expiration headers
1. Control the HTTP header of Apache
The Apache Web Server provides several mechanisms for setting HTTP cache control headers, many of which require an artificial startup.
1. Mod_headers
This module allows you to set the header individually (setting a single HTTP header command to augment the Apache configuration file), and the following is an instance that identifies the file as non-cached:
`
<files *.html>
Header Set Cache-control On-cache
</Files>
`
2. Mod_expries
It provides program logic that automatically generates the expires header with the correct expiration date, and sets the expiration date and caching capability for the file through the module.
3. Mod_cern_meta
2. Controlling the HTML cache via HTTP-EQUIV
The HTTP server responds to the header for the expiration information that is sent to the document and the cache control information, and the Web server interacts with the configuration file to assign the correct Cache-control header to the document provided.
In order for authors to have no need to interact with the Web server's configuration file, HTML2.0 defines the <meta http-equiv> label
`
<Html>
<meta http-equiv= "Cache-control" content= "Cache" >
</Html>
`
However, supporting this may add additional burden to the server, so it is the only reliable way to transfer document cache control by configuring the correct server to issue HTTP headers.
HTTP--Cache