Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use control-cache headers?

I downloaded Google speed tracer for Google chrome to see how my site does performance wise and it tells me I need to enable caching for certain files like my style.css, images, etc.

I've read that the below php code should tell browsers to cache html content. I wrote a quick php page with a couple of images on it and stuck the below code at the top (before the headers are sent) to test to see how it worked.

Header("Cache-Control: public, max-age=3600, must-revalidate");

When I go back to speed tracer's analysis it says...

Summary From Cache: false

Request Headers Pragma: no cache Cache-Control: max-age=0

but under Response Headers... Cache-Control: public, max-age=3600, must-revalidate (exactly what I specified)

I'm a little confused, what's going on...? When it says from cache: false does that mean from the server cache and not the client's cache?

like image 279
payling Avatar asked Apr 07 '10 17:04

payling


People also ask

What are the Cache-Control headers?

What is the Cache-Control Header. Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).

What should you add to a Cache-Control response header?

private. The private response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers). You should add the private directive for user-personalized content, especially for responses received after login and for sessions managed via cookies.

What happens if you don't set Cache-Control header?

Without the cache control header the browser requests the resource every time it loads a new(?) page.


1 Answers

The key is must-revalidate: This means, that the client is asking the server if the file has changed. If you don’t handle this case, the browser will fetch a new copy.

Read Mark Nottingham’s fantastic Caching Tutorial for more information. As an example for a PHP implementation you may use my code.

Look into $_SERVER['HTTP_IF_NONE_MATCH']and $_SERVER['HTTP_IF_MODIFIED_SINCE'] for validating clients. And be aware that both headers may contain malicious code. ;)

like image 155
fuxia Avatar answered Nov 03 '22 23:11

fuxia