This article is a mirror article of machine translation, please click here to jump to the original article.

View: 11711|Reply: 0

[Website O&M] Website Performance Optimization: Detailed explanation of cache-control settings

[Copy link]
Posted on 9/7/2015 7:30:51 PM | | | |


HTTP header Expires and Cache-control


1. Concept

Cache-control is used to control HTTP caching (may not be partially implemented in HTTP/1.0, only Pragma: no-cache is implemented)

Format in the packet:

Cache-Control: cache-directive

cache-directive can be the following:

request:
| "no-cache"
| "no-store"
| "max-age" "=" delta-seconds
| "max-stale" [ "=" delta-seconds ]
| "min-fresh" "=" delta-seconds
| "no-transform"
| "only-if-cached"
| "cache-extension"


response:
| "public"
| "private" [ "=" <"> field-name <"> ]
| "no-cache" [ "=" <"> field-name <"> ]
| "no-store"
| "no-transform"
| "must-revalidate"
| "proxy-revalidate"
| "max-age" "=" delta-seconds
| "s-maxage" "=" delta-seconds
| "cache-extension"

Part of the description:
Divided into cacheable or not
Public indicates that the response can be cached by any cache area.
Private indicates that all or part of a single user's response message cannot be processed by the shared cache. This allows the server to describe only when the user is
Partial response message, which is not valid for requests from other users.
no-cache indicates that the request or response message cannot be cached (HTTP/1.0 is replaced with Pragma's no-cache)
According to what can be cached
No-store is used to prevent important information from being unintentionally published. Sending in a request message will make the request and response messages not cache.
Timeout according to the cache
max-age indicates that the client can receive a response with a lifetime of no greater than a specified time (in seconds).
min-fresh indicates that the client can receive a response with a response time less than the current time plus the specified time.
max-stale indicates that the client can receive response messages beyond the timeout period. If you specify a value for the max-stale message, then the client can
Receive response messages that exceed the specified value of the overtime period.
Expires represent the time of existence, allowing the client not to check (make a request) before this time, which is equivalent to max-age
Effect. But if they exist at the same time, they are overwritten by the max-age of Cache-Control.
Format:
Expires = "Expires" ":" HTTP-date
For example
Expires: Thu, 01 Dec 1994 16:00:00 GMT (必须是GMT格式)

2. Application
Set expires and cache-control via META over HTTP
<meta http-equiv="Cache-Control" content="max-age=7200" />
<meta http-equiv="Expires" content="Mon, 20 Jul 2009 23:00:00 GMT" />
The above settings are just examples, and you can actually use one of them. If you write it this way, it is only valid for the web page, not for images or other requests on the web page, and will not do any cache.
This way, the client will have more requests, and although it is just something that checks the Last-modified status, the number of requests will definitely affect the browsing speed.
If you want to add cache to a file, you can use Apache's mod_expire module, which is written as
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 days"
</IfModule>
I remember that ExpiresActive is set to On, I didn't set it on at first, and it seems that no matter how YSlow is, I can't find the caching mechanism. If you add it like this, it will default to everything.
If you want to target individual MIME types, you can:
ExpiresByType image/gif "access plus 5 hours 3 minutes"
See Apache Module mod_expires
In addition, when you click Refresh on the browser, the requests sent by the client are all max-age=0, indicating that the request is validated and sent to the server
If you ask to check the cache and then update the cache, you will generally get 304 Not Modified, which means that there is no change.






Previous:Find part-time jobs and earn extra money Recruit part-time workers with excellent treatment, cash settlement, more than 300 per day
Next:Like fuzzy query in Linq to sql
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com