Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 speeding up loading.

So what do I need to add to my website to "overwrite" the cache after 3 days to display the latest content?

like image 309
Uli Avatar asked Jun 26 '11 21:06

Uli


People also ask

What is Max-age in Cache-Control?

max-age. The max-age=N response directive indicates that the response remains fresh until N seconds after the response is generated. Cache-Control: max-age=604800. Indicates that caches can store this response and reuse it for subsequent requests while it's fresh.

How do I change my Cache-Control max-age?

Cache-Control: max-age=<seconds> This directive tells the browser or intermediary cache how long the response can be used from the time it was requested. A max-age of 3600 means that the response can be used for the next 60 minutes before it needs to fetch a new response from the origin server.

How do I set Cache-Control in HTML?

To use cache-control in HTML, you use the meta tag, e.g. The value in the content field is defined as one of the four values below. HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.

How long should cache last?

The response can be cached by browsers and intermediary caches for up to 1 day (60 seconds x 60 minutes x 24 hours). The response can be cached by the browser (but not intermediary caches) for up to 10 minutes (60 seconds x 10 minutes). The response can be stored by any cache for 1 year.


1 Answers

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:

  • http://php.net/manual/en/function.header.php
like image 200
james.garriss Avatar answered Oct 26 '22 23:10

james.garriss