original articles, reproduced please specify: reproduced from the electric Shang Sharong ec-shalom.com, specialized in electric business.
The concept of caching has not been clear, resulting in the use of PHP write cache is also very not feeling. This article provides a comprehensive overview of the cache (Interactive encyclopedia | Wikipedia) caches and techniques for writing cache caching using PHP.
- What are cache caches? Why do people want to use it?
- Cache caches types
- Browser Cache Caches
- Proxy Cache Caches
- What is the impact of caching on your site? How do I use caching correctly?
- How the site cache works
- How to set the cache for a Web site Caches
- HTML Meta Tags tags vs. HTTP header headers
- HTTP header headers (why does the HTTP header have no effect on the cache?) )
- HTTP Header parameter Cache-control
- HTTP Headers Setting cache
- Cached validators
- Set up a good method of website caching mechanism
- How to write cache scripts for dynamic websites
- Cache FAQ Daquan
- Implementation of the caching mechanism-Web server-side configuration
- Implementation of the caching mechanism: server-side scripting
- Reference documentation and in-depth reading referencing Reading
- About this article
What are cache caches? Why do people want to use it?
A site that uses cache caches listens to client requests to the server and saves server-side responses-such as HTML pages, images, and so on. Then, if another request is sent using the same URL, he is able to use the feedback file that was previously saved, rather than making a request to the server again.
There are two main reasons for people to use caching:
- Reduce Latency -because a Web request is made to point to a cache that is closer to the client rather than the source server, the request takes less time, which makes the site appear to react faster.
- Reduce Network Load -because cache files can be reused, saving a lot of bandwidth. This also saves users a lot of traffic.
Cache caches types
In modern browsers such as IE, Chrome, Mozilla, and so on, there are cache settings options that can save all the pages you have visited to your local computer's hard drive. You can use this cache when you click on a browser's "back" or a link to a page you've browsed, and if you use a navigation image on your website, the browser cache can respond quickly.
Proxy Cache Caches
The Web Proxy cache uses the same principle, but has a much larger scale, and proxies can serve hundreds of or even thousands of of users in the same way; some large companies and ISPs usually have firewalls or separate devices for them.
Because the proxy cache does not belong to the client or to the server side, it uses network routing to request information. One way to do this is to manually set up the proxy for the browser. You can also use the Web Proxy, the Web proxy directs your URL requests through its potential network to them, so users don't even have to manually configure them.
Proxy caching is a shared cache, and not only one person is using them, but also a large number of users, because they can save bandwidth and web latency very well.
Gateway Cache
As mentioned in the previous "reverse proxy cache" and "proxy cache", the gateway cache is also acting as a proxy role, through the network administrator (or webmaster himself), can make the bandwidth more efficient use, so that their website becomes more reliable.
Content Delivery Networks (CDNs) to sell their cache service. Speedera and Akamai are very famous CDNs.
What is the impact of caching on your site? How do I use caching correctly?
Site cache is often misunderstood, owners think proxy cache can be "hidden" up to their users to take trouble at the same time, and can not update the content of the site in a timely manner. This tutorial can make your site's tutorials more friendly. CDNs Unlike proxy caches, their gateway service caches can be set up according to the website itself. On the other hand, this kind of cache can also speed up the loading speed of your website and improve the quality of user experience.
Many large companies spend $ millions of on server clusters around the world to replicate their site content to ensure that users who use the site are able to maintain the fastest possible response. The cache can do the same for you, and it can even be said that the cache is closer to the end user, and all of this is free.
Whether you like it or not, proxy caching and browser caching are the facts that exist. If you are not able to properly configure your site cache, the site will always keep the cache feature.
How the site cache works
All caches have a whole set of working mechanisms (of course, if the cache is not banned), some of these rules come from the HTTP protocol (extended reading >>) (HTTP 1.0 and 1.1), others from the administrator (possibly a browser, or possibly a proxy).
Generally speaking, they have some common rules:
- The header Response Head section allows you to set whether or not to cache.
- If the request is HTTP authenticated (HTTP Auth extended read >>) or SSL secure link (i.e, HTTPS), the cache does not work.
- The activation of the caching mechanism is caused by a refresh of the page if the following conditions are met (it can be sent directly to the client without the need for a detection server):
- The time does not exceed the set cache page expiration time or other similar age-controlling (indicates that the client can receive a response that is not longer than the specified time).
- The cache was saved on the most recent request, or the time of the cache modification was new.
- When the resource expires (using the Max-age identified by Cache-control) and the discovery Resource has a last-modified (Etag) claim, the request to the Web server If-modified-since (If-none-match)that represents the client request time. Web Server received the request after the discovery of a header if-modified-since(if-none-match) is compared to the last modification time of the requested resource . If the last modification time is newer, indicating that the resource has been changed, then respond to the entire resource content (written in the response message packet), HTTP 200, if the last modification time is older, the resource has no new modifications, the response to HTTP 304 (no package, save browsing), tell the browser to continue to use the saved cache.
- Specific situations-for example, disconnecting from a server-the cache responds directly to the request.
How to set the cache for a Web site Caches
There are tools to help webmasters and developers set up caches for their sites. It may require configuration of your server, more specific content click here implementation.
HTML Meta Tags tags vs. HTTP header headers
We can write the tag tag in the <HEAD> section of the HTML file to describe the properties of the file. Meta Tags can often be used to label files whether the cache is enabled and the cache expiration time is set. Meta tags are easy to use, but inefficient. That's because they're only useful for caching in the browser section, and proxy caching (the proxy never reads the HTML file). If your host is placed on an ISP or a clustered server, they will not give you the right to write the HTTP protocol arbitrarily (ExpiresandCache-Control).
On the other hand, true HTTP headers cannot be seen in HTML files, they are usually generated automatically by the Web server. Even so, you can still control them to some extent, depending on the server on which the site resides. Next, you'll find the HTTP protocol headers very interesting.
HTTP headers has been transmitted before the server sends HTML, and you can only check with browser plugins like Firebug. A typical HTTP 1.1 header response is like this code:
http/1.1 Okdate:fri, OCT 1998 13:19:41 gmtserver:apache/1.3.3 (Unix) cache-control:max-age=3600, Must-revalidateE Xpires:fri, OCT 1998 14:19:41 Gmtlast-modified:mon, June 1998 02:28:12 Gmtetag: "3E86-410-3596FBBC" Content-Length: 1040content-type:text/html
HTML will appear below these header information, and more settings for the HTTP headers method implementation click here.
Pragma http Headers (why does HTTP header have no effect on caching?) )
Many people believe that setting anPragma: no-cacheHTTP protocol can control whether the cache is turned on or not. This is not exactly true. The detailed description of the HTTP protocol does not set any regulations on pragma, whereas pragma requests are highly controversial. Although some caches are affected by this parameter, most of them do not work at all, please use the header header protocol instead!
HTTP Header Control Cache Update
Outdated HTTP protocol headers are the basic meaning of controlling caching. It is able to tell all the cache lifetime time. Over that time, the cache is going to request a server to see if the Web page file has been updated. The header setting of the expiration time supports almost all caches.
Most Web servers allow you to set outdated header information in several ways. Typically, it is allowed to set a certain expiration time (in contrast to the time the user made the latest request), or the last time the Web file was modified on the server side.
The head setting of the expiration time is especially useful for generating static images (such as logos, buttons, etc.). Because they don't have too many changes, you can set a long expiration time on them, making your site more responsive. This approach is also useful for pages that are regularly updated. For example, if you update a news page around 6 o'clock every day, you can set the expiration time to that point in time, so that the cache can be updated even if your users do not click "Reload".
Note: The value of Expires is the date of the HTTP protocol. Any other value will be used as the expired cache time, which will directly cause page caching. The HTTP protocol date is Greenwich Mean Time and not your local time.
For example:
Expires:fri, OCT 1998 14:19:41 GMT
ExpiresIt is important to ensure that your site server time is accurate when you want to use parameters. You can do this with Network time Protocol (NTP). AlthoughExpiresthe expiration time setting is useful, it is still a lot more restrictive, first of all, it involves a time when the site server time must be synchronized with the cache time. Another problem is that you can easily forget some of the pages you've already set up, andExpiresif you don't update before the site expiresExpires, each subsequent request will be returned to your Web server, increasing the load and latency.
HTTP Header parameter Cache-control
HTTP 1.1 (Interactive encyclopedia | Wikipedia) introduced theCache-Controlresponse header parameters to give webmasters more control over the content of the site, while making up for itExpires Limitation.
Cache-ControlThe parameters include:
- max-age= [units: Seconds seconds]-sets the maximum valid time for the cache. similar to Expires, However, this parameter defines the time size (for example: 60) rather than the definite point in time. The unit is [seconds seconds].
- s-maxage= [unit: Seconds seconds]-similar to max-age, but it is only used for the public cache (e.g. proxy). The
- public -response is cached and shared among multiple users. Normally, if http authentication is required, the response is automatically set to private.
The
- private -response can only be used as a private cache (e.g., in a browser) and cannot be shared among users. The
- no-cache -response is not cached, but instead requests resources to the server side in real time. This is useful in ensuring that HTTP authentication strictly disables caching to ensure security is useful (this is when the page is used in conjunction with public). There is no compromise on the efficiency of caching and security.
- no-store -in any condition, the response will not be cached and will not be written to the client's disk, which is also a sensitive response based on security considerations. The
- must-revalidate -response is reused under certain conditions to satisfy the next request, but it must go to the server side to verify that it is still up-to-date.
- proxy-revalidate -similar to must-revalidate, but not for proxy caching.
For example:
cache-control:max-age=3600, Must-revalidate
IfCache-ControlandExpiresat the same time set,Cache-Controlpriority. If you plan to use itCache-Control, don't forget to take a look at the documentation for the HTTP 1.1 protocol. "View References and further information more about >>."
Cached validators
Before we talked about how the site cache works? , the cache validation we are discussing refers to the authentication that is used when the server communicates with the cache when the response resource has changed. With this verification, the cache avoids downloading the entire resource file if there is already a copy of the resource locally.
The validator is important: without it, there is no updated header (ExpiresorCache-Control), and the cache will not be able to store the response resource. Most validators record the time the resource was last modified (in the header parameterLast-Modified), and when the cache holds theLast-Modifiedresponse resource containing the parameters, It compares a request with a parameterIf-Modified-Sincewith the last modification time of a resource on the server to see if the resource has changed.
The HTTP 1.1 protocol also introduces a new authenticator, called the ETag. This validator has a special identifier, which comes from the server and changes as the response resource changes. The simple point is that the server responds to the request URL token, and in the HTTP response header to the client, in the HTTP response header contains the Etag: "5d8c72a5edda8d6a:3239" identity, equals to tell the client, you get this resource has a representation id:5d8c72a5edda8d6a:3239. The next time a request is requested for the same URI, the browser issues a If-none-match header (Http Requestheader) at which the information in the header contains the last access, the ETag: "5d8c72a5edda8d6a : 3239 "Logo:
If-none-match: "5d8c72a5edda8d6a:3239"
This way, the client equals two caches, and the server will have an etag of 2. If If-none-match is False, returns 304 (cache read) and does not return 200 (server Read).
Almost all caches use last-modified as the authenticator, and the etag has a higher priority. Most Web servers will automatically generateETagandLast-Modifiedact as validators for static content (i.e. files); you don't have to make any changes. However, for dynamic content (like CGI, PHP or database sites, etc.) it has no effect; you can see how this article describes how to write cache scripts for dynamic Web sites.
Set up a good method of website caching mechanism
There are a number of ways to make your site cache more friendly:
- Use consistent URLs -This is the golden rule for using caching. If you are providing different users or collecting the same content from different websites to different Web pages, please use a URL address. For example, if you use "/index.html" once, the same naming method is used for other HTML file URLs.
- Use the picture library to see pictures used in different places.
- Create cache to save infrequently changed pictures and pages , you can set the parameter cache-control:max-age The value of the header parameter is larger.
- Sets the timed cache for pages that are regularly updated, and can set the appropriate max-age or expiration time parameters.
- If a resource (especially a downloadable file) changes, its name is changed. In this way, you can allow for a long period of time before it expires, and ensure that the provided version is correct. Pages that are linked to a resource change need to set the cache time to be shorter.
- do not modify the file, unless necessary. If modified at will, the header parameter last-modified will always use the most recent date. For example, when you upgrade the site, do not copy the entire site; Just move the file you modified.
- use cookies only where necessary -cookies is very difficult to cache, and in most cases it is not necessary to cache them. If you must have cookies, limit them to dynamic pages.
- Minimize the use of the SSL security protocol -because encrypted pages will not be stored in a shared cache, try to use SSL when needed, and use as few pictures as possible on SSL pages.
- use redbot to detect your site page -most of the concepts described in this tutorial are useful when using the tool.
How to write cache scripts for dynamic websites
By default, most scripting languages will not return a validator (Last-ModifiedorETagresponse header) or a cache update hint (ExpiresorCache-Control). At the same time, some real scripting languages are dynamic (meaning they return different response resources for each request), and most sites (like search engines and database-driven sites) can benefit from caching scripts.
In general, if you make the same request at the interval of time (whether it's a few days or minutes), you can replicate a script output, then it can be cached. If the content of the script output changes with the URL, it can be cached, but it cannot be cached if the script output relies on cookies, authentication, etc.
- The best way to write a good cache script is to output the contents of the script to a normal file, regardless of when the content is changed, so that the Web site's server can treat it like any other Web page, thus generating and using a cached validator. Remember to write only those files that have changed so thatLast-Modifiedthe values can be protected.
- Another way that the script can be cached is to set a response header for the time of survival, set to the time that the script can use. AlthoughExpires设置可以实现这一点, the settingCache-Control: max-ageis the simplest, it can make a new request for a period of time after a request has occurred.
- If you cannot do this, you will need to have the script generate a validator and then make a response to the browserIf-Modified-Sinceand/or to the requestIf-None-Match. By parsing the HTTP header you can do this by returning 304 (cache read) response status as appropriate.
Tips:
- if not necessary, try not to use the Post method . Most caches cannot keep a response to the post, you have to send some information through the path or request (GET), the cache can store information in the future.
- do not use URLs to pass user identity information unless the entire content is to be presented to the full user .
- do not expect all requests from one user to be sent from the same host because the cache often works together.
- set in the response headerContent-Length(used to describe the transmission length of the HTTP message entity). It allows your scripts to still respond to long-connected communications, and clients can request different resources over a TCP/IP connection, rather than establishing a single communication for a single request.
Click here for more information.
Cache FAQ What is it that needs to be set to cache?
There is a good strategy to determine the maximum number of visitors to the site, the amount of data (especially pictures) the largest part.
How do I use caching to make my pages as fast as possible?
Most cache response resources set a refresh request time, and cache validation can help you reduce the time to see if the resource is updated, but the cache always needs to communicate with the server to check for updates.
I already know the advantages of caching, but I need to count traffic data for my page!
If you have to know the page each time the data is browsed, you can set a small section on the page to not cache, for example, a 1*1 size picture on each page, the relevant response header will contain the information on the page.
Considering that this does not only make it impossible to count the exact user data and is harmful to the network and the user (generating invalid traffic, increasing the delay time), you may consider using third-party statistical tools related information >>.
How do I view the HTTP protocol header?
Many browsers can view and head parameters in the page informationExpiresLast-Modified, or use Chrome or Mozilla's firebug.
If you respond to more complete response header information, you can use the Telnet client to connect to the Web site, as follows:
First step: In the Start menu, click on the "Run" command and enter "Telnet ec-shalom.com:80".
Step two: Turn on the local display. Enter the address of the response header you want to view, for example, you want to view the page http://ec-shalom.com/683_webfilecoding.html
get/683_webfilecoding.html http/1.1 [return]host:ec-shalom.com [Return][return]
Every time[return] Press enter you see it, you end up hitting it two times. It will print all header information, and response resources, if only the head information, the get to head.
My pages are password protected, how will the proxy cache handle them?
By default, pages set up for HTTP authentication are treated as private, and they will not be saved by the shared cache. However, you can set the parameter Cache-control on the head of the page to achieve the goal; HTTP 1.1-The protocol will be cached.
If you want this page to be cached, but can verify each user, willCache-Control: publicandno-cachecombine. It allows the client to send authentication information that reads the cached content before connecting to the database:
Cache-control:public, No-cache
In any case, try to use less validation as much as possible, for example, if the pictures on your page are not necessarily verified, you put them in a separate folder and configure your server to not validate it.
Is it safe to browse my site through the cache?
SSL pages on the proxy server will not be cached (not recommended), so you don't have to worry about it. However, because the cache holds non-SSL requests and URLs crawled from them, you have to be aware that there are no security-protected sites that may be unethical administrators who may collect user privacy, especially through URLs.
In fact, administrators located between the server and the client can collect this kind of information. In particular, the use of CGI scripts to pass the user name and password through the URL is a big problem, which is a large vulnerability to the disclosure of user names and passwords;
If you have an initial understanding of the security mechanisms of the Internet, you will not have any of the cache servers.
I am looking for a system that is included in the Web publishing system, those that are more cache aware?
This is hard to say, generally the more complex the system the more difficult the cache. The worst is full dynamic release does not provide validation parameters; You do not cache any content. You can check with your system provider's technician and refer to the implementation instructions later.
My picture is set to expire after 1 months, but I now need to update it now.
The expiration time is not the same, unless the cache (browser or proxy server) does not have enough space to delete the replica, and the cached copy is always used between expiration.
The best way is to change their links so that the new copy will be re-downloaded from the source server. Remember: the pages that reference them are also cached. Therefore, using static pictures and similar content is very easy to cache, and the reference to their HTML page is to maintain a very updated;
If you want to reload a copy of the specified cache server, you can force the use of "refresh" (hold down the SHIFT key when reload in Firefox: There will be a previous reference to the bad Pragma:no-cache header). Or you can have the cache administrator remove the content from their interface;
I run a web hosting service, how do I get my users to publish cache-friendly pages?
If you use Apahe, consider allowing them to use the. htaccess file and provide the appropriate documentation;
On the other hand: You can also consider establishing various caching strategies on various virtual hosts. For example, you can set up a directory/cache-1m specifically for access for 1 months, and another/no-cache directory to provide services that do not store replicas.
In any case: the cache should be used for a large number of user accesses. For large sites, this savings is obvious (bandwidth and server load);
I flagged some pages to be cacheable, but the browser still sends the request to the service every time. How do I force them to save a copy?
The cache server does not always save replicas and reuse replicas, and they simply do not save and use replicas under certain circumstances. All cache servers are back based on the size of the file, the type (for example: Picture page), or the remainder of the server space to determine how to cache. Your page is not worth caching compared to more popular or larger files.
So some cache servers allow administrators to prioritize cache replicas based on file type, allowing certain replicas to be permanently cached and long-term valid;
Implementation of the caching mechanism-Web server-side configuration
Generally, you should choose the latest version of the Web server program to deploy. New versions tend to have a lot of improvements in performance and security, not only because they contain more caching-enabled features.
Apache HTTP Server
Apache has some optional modules to contain these header information: including expires and Cache-control. These modules are supported in the 1.2 version above;
These modules need to be compiled with Apache, although they are already included in the release version, but are not enabled by default. In order to determine that the corresponding module has been enabled: Find the HTTPD program and run Httpd-l it will list the available modules, the modules we need to use are mod_expires and mod_headers
- If these modules are not available, you need to contact the administrator to recompile and include the modules. These modules are sometimes enabled by using the commented out configuration file, or by adding the-enable-module=expires and-enable-module=headers options at compile time (in Apache 1.3 and later). Refer to the install file in the Apache release version;
Apache once the corresponding module is enabled, you can set the copy to expire in the. htaccess file or in the server's access.conf file by Mod_expires. You can set the expiration to be calculated from the access time or file modification time, and apply to a file type or default setting, refer to the module's documentation for more information, or ask the Apache expert around you when you are having problems.
To apply Cache-control header information, you need to use Mod_headers, which will allow you to set any HTTP header information, refer to Mod_headers's documentation for more information;
Here's an example of how to use header information:
- The. htaccess file allows Web publishers to use commands that are used only in configuration files. He affects the directory and its subdirectories; Ask your server administrator to confirm that the feature is enabled.
# # # Enable Mod_expires
Expiresactive on
# # # set. gif expires 1 months after being visited.
Expiresbytype Image/gif A2592000
# # # Other files are set to expire after 1 days after the last modified time
# # # (with a different syntax)
ExpiresDefault "modification plus 1 day"
# # # Application Cache-control Header properties in index.html file
<files index.html>
Header Append Cache-control "public, must-revalidate"
</Files>
- Note: Mod_expires automatically calculates and inserts Cache-control:max-age header information when appropriate
Apache 2.0 configuration and 1.3 Similar, more information can refer to the 2.0 mod_expires and mod_headers documents;
Microsoft IIS Server
Microsoft IIS can set the header information very easily, note that this is only for IIS 4.0 servers and only runs on NT servers.
To set header information for a region of a site, first go to the Administrator tool interface, and then set the properties. Select the HTTP header menu and you'll see 2 interesting areas: Enable Content Expiration and customize HTTP header information. The first setting is automatically configured, and the second one can be used to set the Cache-control header information;
The header information for the ASP page can be referenced in the following ASP section, or the header information can be set through the ISAPI module, please refer to MSDN for details.
Netscape/iplanet Enterprise Server
After version 3.6, Netscape/iplanet has been unable to set expires header information, and he has supported HTTP 1.1 functionality since version 3.0. This means that the cache (proxy server/browser) advantage of HTTP 1.1 is available through your Cache-control settings.
Using Cache-control header information, select Content management on the Management Server | Cache Settings directory. Then: Using the resource selector, select the directory where you want to set the header information. After setting the header information, click "OK". For more information, refer to the Netscape/iplanet Enterprise Server manual.
Implementation of the caching mechanism: server-side scripting
One thing to note is that it may be easier for the server to set HTTP header information than the scripting language, but you should use both.
Because the server-side script is primarily for dynamic content, he does not produce cacheable file pages by itself, even if the content is actually cacheable. If your content changes frequently, but not every time the page request changes, consider setting up a cache-control:max-age header, and most users will visit the same page multiple times in a short period of time. For example: users click the "Back" button, even if there is no new content, they still have to download the content from the server to view.
CGI Programs
CGI scripts are one of the most popular ways to generate content, and you can easily extend the HTTP header information before sending content; Most CGI implementations require you to write Content-type header information, such as this Perl script:
#!/usr/bin/perl
print "content-type:text/html\n";
Print "Expires:thu, OCT 1998 17:04:19 gmt\n";
print "\ n";
# # # Behind is the content body ...
Because it's all text, you can easily generate expires and other date-related header information through built-in functions. If you use Cache-control:max-age, it will be easier;
print "cache-control:max-age=600\n";
This allows the script to be cached for 10 minutes after being requested, so that if the user presses the Back button, they will not resubmit the request;
The CGI specification also allows the client to send header information, each with a ' http_ ' prefix, so that if a client sends a if-modified-since request, that is:
Http_if_modified_since = Fri, Oct 1998 14:19:41 GMT
Refer to the Cgi_buffer library, a library that automatically processes the generation and validation of the ETag, generates Content-length attributes, and compresses the content. Only one line is added to the Python script;
Server-side contains server Side includes
SSI (often using the. shtml extension) is the first scenario that a site Publisher can generate dynamic content. It also becomes a kind of embedded HTML script by setting special tags in the page;
Most implementations of SSI cannot set a validator and cannot be cached. However, Apache can allow the user to set the kind of SSI that can be cached by implementing the permission settings for a specific set of files, and xbithack the entire directory. For more documentation, refer to the Mod_include documentation.
Php
PHP is a server-side scripting language built into the Web server, as an HTML embedded script, much like SSI, but with more options, PHP can be set to run in CGI mode on various Web servers, or as an Apache module;
The default PHP build replica does not have a validator set and cannot be cached, but developers can generate HTTP header information via the header () function.
For example: The following code generates a Cache-control header message and sets the expires header information to expire after 3 days;
<?php
Header ("Cache-control:must-revalidate"), $offset = 60 * 60 * 24 * 3;
$EXPSTR = "Expires:". Gmdate ("D, D M Y h:i:s", Time () + $offset). "GMT";
Header ($EXPSTR);
?>
Remember: The output of the Header () must precede the output of all other HTML;
As you can see: You could create the HTTP date manually; PHP does not provide you with a special function (the new version has made this more and more easy, please refer to the PHP date-related function document), of course, the simplest is to set the Cache-control:max-age header information, And it is more applicable to most cases;
For more information, refer to the header related documentation;
Also refer to the Cgi_buffer library, automatic processing of etag generation and validation, content-length generation and content gzip compression, PHP script only contains 1 lines of code;
Cold Fusion
Cold Fusion is Macromedia's commercial server-side scripting engine and supports a variety of Windows platforms, Linux platforms, and multiple UNIX platforms. Cold Fusion is relatively easy to set HTTP header information with cfheader tags. Unfortunately, the following expires header information is easy to mislead;
<cfheader name= "Expires" value= "#Now () #" >
It's not working as you think, because the time (in this case, the time the request originated) is not converted to a date/time object that meets the HTTP time and prints out the copy of Cold fusion, and most clients ignore it or convert it to January 1, 1970.
However: Cold Fusion also provides a set of date formatting functions, gethttptimestring. Combined with the DateAdd function, it is easy to set the expiration time, here we set a header declaration copy after 1 months expires;
<cfheader name= "Expires" value= "#GetHttpTimeString (DateAdd (' m ', 1, Now ())) #" >
You can also use the Cfheader tag to set other header information such as Cache-control:max-age;
Remember: The Web server will also transfer the header information settings to Cold fusion (as a CGI runtime), check your server settings and determine if you can use server settings instead of Cold fusion.
ASP and ASP.
Setting HTTP header information in ASP is: Confirm that the Response method is called before the HTML content output, or use the Response.Buffer staging output; Note that some versions of IIS default settings are output cache-control:private Header information must be declared public to be shared by the cache server cache.
IIS's ASP and other Web servers allow you to set the HTTP header information, for example: Set the expiration time, you can set the properties of the response object;
<% response.expires=1440%>
Set the requested copy to expire after a specified minute of output, similar to: You can also set an absolute expiration time (verify that your HTTP date is formatted correctly)
<% response.expiresabsolute= #May 31,1996 13:30:15 gmt#%>
Cache-control header information can be set like this:
<% response.cachecontrol= "Public"%>
In ASP. Response.Expires is deprecated, the correct way is to set the cache-related header information through Response.Cache;
Response.Cache.SetExpires (DateTime.Now.AddMinutes (60));
Response.Cache.SetCacheability (Httpcacheability.public);
Refer to the MSDN documentation to find out more about the New Year series;
Cache caching mechanism and file caching principle PHP2