Cache-Control
Cache-Control is the most important rule. This field is used to specify the commands that all cache mechanisms must obey throughout the request/response chain. These commands are used to prevent the cache from adversely interfering with requests or responses. These commands usually overwrite the default cache algorithm. Cache commands are one-way, that is, there is a command in the request does not mean that the same command will exist in the response.
The definition of cache-control is: Cache-Control = "Cache-Control" ":" cache-direve ve. Table 1 shows applicable values.
Table 1. Common cache-direve ve values
Cache-directive |
Description |
Public |
All content will be cached |
Private |
Content is only cached in private Cache |
No-cache |
All content will not be cached |
No-store |
All contents are not cached in the cache or Temporary Internet Files. |
Must-revalidation/proxy-revalidation |
If the cached content is invalid, the request must be sent to the server/proxy for reverification. |
Max-age = xxx (xxx is numeric) |
The cached content will expire after xxx seconds. This option is only available in HTTP 1.1 and has a higher priority if used together with Last-Modified. |
Table 2 indicates whether the browser resends the request to the server or uses the cached content in different circumstances.
Table 2. browser response to cache-ctictive values
Cache-directive |
Open a new browser window |
Click Enter in the original window |
Refresh |
Click Back |
Public |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser resends the request to the server. |
The browser displays the page from the cache |
Private |
The browser resends the request to the server. |
For the first time, the browser resends the request to the server. After that, the browser displays the page from the cache. |
The browser resends the request to the server. |
The browser displays the page from the cache |
No-cache/no-store |
The browser resends the request to the server. |
The browser resends the request to the server. |
The browser resends the request to the server. |
The browser resends the request to the server. |
Must-revalidation/proxy-revalidation |
The browser resends the request to the server. |
For the first time, the browser resends the request to the server. After that, the browser displays the page from the cache. |
The browser resends the request to the server. |
The browser displays the page from the cache |
Max-age = xxx (xxx is numeric) |
After xxx seconds, the browser resends the request to the server |
After xxx seconds, the browser resends the request to the server |
The browser resends the request to the server. |
After xxx seconds, the browser resends the request to the server |
Cache-Control is the most important setting about browser Cache, because it overwrites other settings, such as Expires and Last-Modified. In addition, because the browser behavior is basically the same, this attribute is the most effective way to deal with cross-browser cache problems.
Invalid
The Expires header field provides a date and time, and the response is considered invalid after the date and time. Invalid cache entries are generally not returned by the cache (whether the proxy cache or the user Proxy Cache) unless they are verified by the original server (or the intermediary cache with the latest copy of the entity. (Note: cache-control max-age and s-maxage will overwrite the Expires header .)
The Expires field receives values in the following format: "Expires: Sun, 08 Nov 2009 03:37:26 GMT ". If the date when the content is viewed is earlier than the specified date, the content is considered invalid and extracted from the cache. Otherwise, the cache takes some measures. Table 3-6 shows the actions of different browsers for different user operations.
Table 3. Invalid operations when a user opens a new browser window
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content is not invalid. |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 200. |
The browser displays the page from the cache |
The browser displays the page from the cache |
Invalid Content |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Table 4. Invalid operations when you click Enter in the original browser window
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content is not invalid. |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
Invalid Content |
The browser resends the request to the server. The Returned Code is 200. |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Table 5. failed operations when you press F5 to refresh the page
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content is not invalid. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
Invalid Content |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Table 6. Invalid operations when you click the Back or Forward button
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content is not invalid. |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser displays the page from the cache |
Invalid Content |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 200. |
Note: all browsers are assumed to run with the default settings.
Last-Modified/E-Tag
The field value of the Last-Modified object header is usually used as a cache validator. To put it simply, if the object value is not changed after the Last-Modified value, the cache entry is considered valid. The field value in the ETag response header is an entity tag that provides an "Opaque" cache validator. This may provide more reliable verification in the following situations: it is not convenient to store the modified date; the one-second solution for the HTTP date value is insufficient; or the original server wants to avoid some conflicts caused by the date of modification.
Different browsers have different configuration behaviors. Table 7-10 shows the actions performed on different browsers for different users.
Table 7 Last-Modified E-Tag operation when a user opens a new browser window
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content has not been modified since the last access. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The content has been modified since the last access. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Table 8 Last-Modified E-Tag operation when you click Enter in the original browser window
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content has not been modified since the last access. |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The content has been modified since the last access. |
The browser resends the request to the server. The Returned Code is 200. |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Table 9. Last-Modified E-Tag operation when you press F5 to refresh the page
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content has not been modified since the last access. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The browser resends the request to the server. The Returned Code is 304. |
The content has been modified since the last access. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Table 10. No cache settings and users click Back or Forward
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
The content has not been modified since the last access. |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser displays the page from the cache |
The content has been modified since the last access. |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser displays the page from the cache |
The browser resends the request to the server. The Returned Code is 200. |
Note: all browsers run with the default settings.
No cache settings
If you do not define any cache-related settings, different browsers may behave differently. Sometimes, the behavior of the same browser is different in each running case. The situation may be complicated. In addition, if some content that should not be cached is cached, security issues may occur.
Different browsers have different behaviors. Table 11 shows different browser behaviors.
Table 11. No cache settings and the user opens a new browser window
|
Firefox 3.5 |
IE 8 |
Chrome 3 |
Safari 4 |
Open a new page |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Click Enter in the original window |
The browser resends the request to the server. The Returned Code is 200. |
The browser displays the page from the cache. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Press F5 to refresh |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Click Back or Forward. |
The browser displays the page from the cache. |
The browser displays the page from the cache. |
The browser resends the request to the server. The Returned Code is 200. |
The browser resends the request to the server. The Returned Code is 200. |
Note: all browsers run with the default settings.
Finally, let's summarize the key conclusions:
Key conclusions
Open new window |
If the cache-control values are private, no-cache, and must-revalidate, the server will be accessed again when a new window is opened. If the max-age value is specified, the server will not be accessed again during this period, for example: Cache-control: max-age = 5 indicates that the server will not be accessed again within 5 seconds after the webpage is accessed. |
Press enter in the address bar |
If the value is private or must-revalidate, the server will be accessed only during the first access and will not be accessed in the future. If the value is no-cache, it will be accessed every time. If the value is max-age, the access will not be repeated before expiration. |
Back and press the button |
If the values are private, must-revalidate, and max-age, no re-access will be performed. If the value is no-cache, the access will be repeated each time. |
Press the refresh button |
No matter what the value is, it will be accessed again. |