Usually you can achieve significant reduction in server load if you’re able to specify how long your dynamic web pages remain valid. If you set an explicit expiration date on a web page, browsers use a cached copy until the specified expiration date (check this set of dynamic web pages).
Typically you can safely set the expiration date a few hours or even days in the future, although applications listed in the previous section are obviously excluded. For example, the article you’re reading right now could have the expiration date set to next Monday (as the "You May Also Like" and "Promotions" tabs don’t change very often). However, the reader comments at the bottom should be refreshed more often. In such cases, it’s best to separate the dynamic add-ons (such as banners, promotions, or user comments) from the static content of the page and load them separately
The explicit content expiration is specified with the Expires header (HTTP/1.0 and HTTP/1.1 clients) or with the max-age option of the Cache-control header. HTTP/1.0 clients ignore the Cache-control header, as it’s part of the HTTP/1.1 standard, and HTTP/1.1 clients ignore the Expires value if the max-age parameter of the Cache-control header is present.
For example, to set a web page to expire in 24 hours, use the following command in ASP (the Expires property takes delta time in minutes):
<% Response.Expires = 1440 %> or <% Response.CacheControl = "max-age=86400; public" %>
The ASP.NET @ OutputCache directive makes your task even simpler:
<%@ OutputCache Duration="86400" Location="Any" %>
In environments where you have to set the HTTP header manually (for example, when using PHP), the header choice is also somewhat influenced by your expiration rules, as the Expires header contains an absolute date and the max-age parameter contains delta time in seconds:
- If your content expires on a specific date (for example, all articles expire Sunday night, when the new content is published), the Expires header is easier to use.
- If you want to specify a relative expiration period and don’t have to support HTTP/1.0 clients (they’re rare, anyway), use the max-age parameter.