Skip to content Skip to sidebar Skip to footer

HTML - Cache Control Max Age

I'ld like to present always the latest website content to the user but also have it fast loaded. By researching I came across postings people suggesting to use the cache for speedi

Solution 1:

The Cache-Control header is used in HTTP 1.1 to control the behavior of caches. The max-age directive is used to specify (in seconds) the maximum age of the content before it becomes stale (i.e., the content will not change for some period of time). So if you know that your content will not change for 3 days, you want your server to add the following HTTP header:

Cache-Control: max-age=259200

(259200 = 60s x 60m x 24h x 3d)

To do that in PHP, add this line to your output:

header('Cache-Control: max-age=259200');

Read here for more info on the header function:


Solution 2:

There is more than one way to do this - but you need to consider exactly what you need to cache and what you don't. The biggest speed increases will likely come from making sure your assets (css, images, javascript) are cached, rather than the html itself. You then need to look at various factors (how often do these assets change, how will you force a user to download a new version of the file of you do change it?).

Often as part of a sites release process, new files (updated files) are given a new filename to force the users browser to redownload the file, but this is only one approach.

You should take a look at apache mod_expire, and the ability to set expiry times for assets using the .htaccess file.

http://www.google.com/?q=apache+cache+control+htaccess#q=apache+cache+control+htaccess


Solution 3:

As mentioned Expires and Cache-Control Headers are usually the best way to incorporate information about information lifetime.

Because clients are not very reliable on interpreting these informations proxies with caching capabilities like squid, varnish or such solutions are preferred by most people. You also need to consider if you want to cache only static content (like images, stylesheets, ..) or dynamically generated content as well.


Solution 4:

As per the YSlow recommendations you could configure your web server to add an Expires or a Cache-Control HTTP header to the response which will result in user agents caching the response for the specified duration.


Post a Comment for "HTML - Cache Control Max Age"