"Cache-control" and Java Configuration

Source: Internet
Author: User

The cache of web pages is controlled by "cache-control" in the HTTP message header. common values include private, no-cache, Max-age, and must-revalidate, the default value is private. The function of browser review is divided into the following situations:
(1) open a 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 within the time range. For example:
Cache-control: Max-age = 5
Indicates that the webpage will not go to the server again within 5 seconds after it is accessed.
(2) Press enter in the address bar.
If the value is private or must-revalidate (not the same as on the Internet), the server will be accessed only when the first access is made and will not be accessed later. 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.
(3) 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.
(4) press the refresh button
No matter what the value is, it will be accessed again

When the cache-control value is "no-Cache", accessing this page will not leave page backup in the temporary article folder on the Internet.
In addition, specifying the "expires" value also affects the cache. For example, if you specify the expires value as a time that has passed, if you repeatedly press enter in the address bar when accessing this network, the access will be repeated each time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT

In ASP, the expires and expiresabsolute attributes of the response object can be used to control the expires value. The cachecontrol attribute of the response object can be used to control the cache-control value. For example:
Response. expiresabsolute = #2000-1-1 # 'specifies the absolute expiration time, which is the local time of the server and is automatically converted to GMT.
Response. expires = 20' specifies the relative expiration time, in minutes, which indicates how many minutes have expired since the current time.
Response. cachecontrol = "no-Cache"
The expires value can be viewed by viewing the properties of the temporary file in the Temporary Internet folder.

Data packet format:

Cache-control: cache-Directive

Cache-directive can be:

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"

Some instructions:

Divided
Public indicates that the response can be cached in any cache area.
Private indicates that the whole or part of the response message of a single user cannot be processed by the shared cache. This allows the server to only describe a user's
Partial response message, which is invalid for other users' requests.
No-Cache indicates that the request or response message cannot be cached (HTTP/1.0 is replaced by no-cache of Pragma)
Based on what can be cached
No-store is used to prevent the unintentional release of important information. Sending a request message does not cache the request and response messages.
Cache timeout
Max-age indicates that the client can receive responses with a lifetime not greater than the specified time (in seconds.
Min-fresh indicates that the client can receive a response whose response time is earlier than the current time plus the specified time.
Max-stale indicates that the client can receive response messages beyond the timeout period. If the value of the Max-stale message is specified, the client can
Receive response messages that exceed the timeout period.
Expires indicates that there is a time, allowing the client not to check (send a request) before this time, equivalent to the max-age
Effect. If both exist, it will be overwritten by the Max-age of cache-control.
Format:
Expires = "expires" ":" http-Date
For example
Expires: Thu, 01 Dec 1994 16:00:00 GMT (must be in GMT format)
Set expires and cache-control through HTTP Meta

HTML code
  1. <Meta http-equiv = "cache-control" content = "Max-age = 7200"/>
  2. <Meta http-equiv = "expires" content = "mon, 20 Jul 2009 23:00:00 GMT"/>
<meta http-equiv="Cache-Control" content="max-age=7200" /> <meta http-equiv="Expires" content="Mon, 20 Jul 2009 23:00:00 GMT" />

The preceding settings are only for example. This statement is only valid for the webpage and does not cache images or other requests on the webpage.
In this way, the client has more requests. Although only the last-modified status is checked, the browsing speed must be affected when many requests exist.
If you want to add a cache to a file, you can use the mod_expire module of Apache, which is written
<Ifmodule mod_expires.c>
Expiresactive on
Expiresdefault "access plus 1 days"
</Ifmodule>
I remember setting expiresactive to on. I didn't set on at first. It seems that yslow cannot find the cache mechanism. In this way, the default value is all.
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, all requests sent by the client are Max-age = 0, which indicates the validate operation and send the request to the server.
Check the cache and update the cache. Generally, the result is 304 not modified, indicating no change.
Use a filter in the project to set the webpage Cache

Filterconfig FC;

Public void dofilter (servletrequest req, servletresponse res,
Filterchain chain) throws ioexception, servletexception {
Httpservletresponse response = (httpservletresponse) RES;
// Set the provided HTTP Response parameters
For (enumeration E = FC. getinitparameternames (); E. hasmoreelements ();){
String headername = (string) E. nextelement ();
Response. addheader (headername, FC. getinitparameter (headername ));
}
// Pass the request/response on
Chain. dofilter (req, response );
}

Configuration File: <filter>
<Filter-Name> nocache </filter-Name>
<Filter-class> filter. cachefilter </filter-class>
<Init-param>
<Param-Name> cache-Control </param-Name>
<Param-value> NO-cache, must-revalidate </param-value>
</Init-param>
</Filter>
<Filter>
<Filter-Name> cacheforweek </filter-Name>
<Filter-class> filter. cachefilter </filter-class>
<Init-param>
<Param-Name> cache-Control </param-Name>
<Param-value> MAX-age = 604800 </param-value>
</Init-param>
</Filter>

<Filter-mapping>
<Filter-Name> cacheforweek </filter-Name>
<URL-pattern> *. js </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> cacheforweek </filter-Name>
<URL-pattern> *. CSS </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> cacheforweek </filter-Name>
<URL-pattern> *. gif </url-pattern>
</Filter-mapping>

The preceding settings Save the cache for one week.

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.