A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
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.
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:
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.
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.
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.
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.
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:
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.
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.
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!
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.
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 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:
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 >>."
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:
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.
There are a number of ways to make your site cache more friendly:
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.
Click here for more information.
There is a good strategy to determine the maximum number of visitors to the site, the amount of data (especially pictures) the largest part.
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.
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 >>.
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.
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:
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.
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.
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.
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;
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);
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;
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 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
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:
Apache 2.0 configuration and 1.3 Similar, more information can refer to the 2.0 mod_expires and mod_headers documents;
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.
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.
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 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:
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;
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:
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;
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 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;
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 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;
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;
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.
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;
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)
Cache-control header information can be set like this:
In ASP. Response.Expires is deprecated, the correct way is to set the cache-related header information through Response.Cache;
Refer to the MSDN documentation to find out more about the New Year series;
Cache caching mechanism and file caching principle PHP2
Start building with 50+ products and up to 12 months usage for Elastic Compute Service