Jump to content

Cache headers


Drongo_III

Recommended Posts

Hi Guys

 

I have recently been looking into implementing browser caching after reading this: https://developers.google.com/speed/docs/best-practices/caching 

 

There are a few things in that google article that don't make much sense to me so I'm hoping someone can clarify.

 

Firstly Question

 

The document says:

 

" we recommend that you configure your web server to explicitly set caching headers and apply them to all cacheable static resources"

 

This implies you can set caching headers for just some resources. How would you do this? Lets assume I only wanted to set caching headers for a file styles.css - would I need to output headers within the CSS file and then change the extension to .php? e.g.

<link rel="stylesheet" href="styles.php"/>

Second question

 

Etags have me very confused. How would you set etag headers for specific files and then generally? And if you set the etag in the header of the main php page does that mean everything gets cached?

 

 

Any help someone can provide would be very, very welcome!

 

Drongo

Link to comment
Share on other sites

Thanks jacques.

 

I'm still confused as to how etags are implemented on a per resource basis. I realise it can be implented through htaccess but how would you define it for a particular resource ?

 

Sorry if I am being slow on this but nothing I read on google leaves me very enlightened.

Link to comment
Share on other sites

You don't set ETag values for individual files. You enable it for a set of files, be that *.* or *.jpg or whatever, and tell the web server how to calculate the value. For example, Apache can calculate the value based on the file modification time and/or size and/or inode.

Link to comment
Share on other sites

I'm still confused as to how etags are implemented on a per resource basis. I realise it can be implented through htaccess but how would you define it for a particular resource ?

If you wanted to set it for a specific file you'd use the <Files> block to limit it's scope, eg:

<Files somefile.css>
    FileETag MTime Size
</Files>
Edited by kicken
Link to comment
Share on other sites

Hi Guys

 

Thanks for sticking with me whilst I try to grasp what is probably an overwhelmingly simple concept...however...

 

This is what has me confused. Excerpt from the google page I referenced:

 

 

 

Last-Modified and ETag. These specify some characteristic about the resource that the browser checks to determine if the files are the same. In the Last-Modified header, this is always a date. In the ETag header, this can be any value that uniquely identifies a resource (file versions or content hashes are typical).

 

So what I don't understand is what's the value that "uniquely identifies a resource..." when you place the following in your htaccess? How does the eTag get it's unique identifier? Is it set automatically somehow?

<Files somefile.css>
    FileETag MTime Size
</Files>
Edited by Drongo_III
Link to comment
Share on other sites

The server will generate the ETag based on various file attributes. Which attributes it considers is defined by the FileETag directive. As set in the example, it would use the files last modified date and the size of the file to generate the identifier.

 

How it uses those values to generate the tag is up to it's implementation details. If you were developing a script that needed to generate an ETag you might use something like:

$etag = sha1(filemtime($file).'-'.filesize($file));
header('ETag: '.$etag);
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.