asp.net client browser Cache HTTP Headers Introduction _ Practical Tips

Source: Internet
Author: User
Tags current time http authentication local time time interval browser cache
Having the browser do the caching requires sending a specified HTTP header to the browser, telling the browser how long the cache is cached, or not caching it. As a. NET programmer, we have always used this method, in the outputcache instructions to specify the cached location as client, is actually to the browser sent an HTTP header, tell the browser how long the URL to cache, the last modified time.

Microsoft has a good encapsulation of the HTTP headers used in OutputCacheModule, but knowing these HTTP headers can be used more flexibly.

There are several HTTP headers associated with the client cache, respectively:

1. expires:+ Expiration Time
Indicates that the browser cache is invalidated after a specified time, and it is important to note that the expiration time here must be the HTTP format date time, the other will be resolved to the current time "before", the cache will expire immediately, and the HTTP date time must be GMT (GMT), not local time. Example:
Copy Code code as follows:

Expires:fri OCT 2009 14:19:41

Using expires expiration must require the server's time to be correct, otherwise the HTTP headers sent will be problematic, and a time server can be set up to synchronize time under Windows services.

2. Cache-control:
Cache-control literal translation into Chinese is the cache control, it is the role of caching control, the HTTP header has several values.
1 max-age=[sec]-Execution Cache is considered to be the most recent for the longest time. Similar to the expiration time, this parameter is based on the relative time interval of the request time, not the absolute expiration, and [SEC] is a number, in seconds: the number of seconds between the time the request begins and the expiration time.
2 s-maxage=[sec]-is similar to the Max-age property except that he is applied to a shared (e.g. Proxy server) cache
3 public-Mark Certification content can also be cached, in general: After HTTP authentication to access the content, the output is automatically can not be cached;
4) no-cache-forces each request to be sent directly to the source server without a local cached version of the checksum. This is useful for identifying authentication applications (which can be used in conjunction with public) or demanding the use of the latest data applications (regardless of the benefits of using caching);
5 no-store-forced to hold no copies in any case
6 must-revalidate-tells the cache to follow all the freshness you give to the replica, HTTP allows the cache to return expired data in certain cases, specify this attribute, you cache, you want to strictly follow your rules.
7 proxy-revalidate-and must-revalidate are similar except that he only works on the caching proxy server
Example:
Copy Code code as follows:

cache-control:max-age=3600, Must-revalidate

Obviously Cache-control can provide more flexible cache control than expires, and it does not need to be dependent on server time.
In ASP.net, Microsoft has encapsulated the Cache-control attribute's settings into the HttpCachePolicy class, and we can invoke the following methods to control the Cache-control HTTP header value by Response.Cache:
Copy Code code as follows:

Response.CacheControl;
Response.Cache.SetNoStore
Response.Cache.SetMaxAge
Response.Cache.SetProxyMaxAge
Response.Cache.SetRevalidation

3. Last-modified/if-modified-since
These two HTTP headers are a pair that represents the most recent update time for an address, which is the server-side response to the client, and the latter is sent to the server by the client browser, telling the Web server that the client has a cache of why the time was last changed. When the server side receives the if-modified-since header, it determines if the cache of this URL address is up to date, and if the server side returns Httpstatus 304 directly to the client, it means that the content has not changed since your last request, You can use the cache directly; If the server finds that the URL was last updated more than the If-modified-since value, the new content is output.

Microsoft also made the server-side package for us, so we can call
Copy Code code as follows:

Response.Cache.SetLastModified (DateTime)
Response.Cache.SetLastModifiedFromFileDependencies ()

If you have more complex requirements, you need to deal with them yourself.

4. Etag/if-none-match
ETag and last-modified are similar, but he sends a string to indicate the version of the URL, and if the URL changes, the label will change, and when the browser sends the If-none-match, it tells the browser that the content has changed, or that the cache can be used.

IIS automatically adds etag to the static file and regenerates a etag when the file changes, so that the client can download just one time for the n-Multiple static files in a Web site, such as style sheets, small pictures, and so on, to lighten the load.

In asp.net we can use the following two methods to set the
Copy Code code as follows:

Response.Cache.SetETag (String)
Response.Cache.SetETagFromFileDependencies ()

Although Microsoft has done a lot of encapsulation for us, we still need a detailed understanding before we can use these several HTTP headers.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.