Forum Moderators: coopster

Message Too Old, No Replies

Is this the optimal and minimal way to properly cache files?

         

JAB Creations

7:19 pm on Dec 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I noticed that the page speed extra for Firebug wasn't too keen about how I was handling cache. I spent some time and I think I've been able to minimize the code necessary for successful caching. With a real time Apache access log (Tail for Win32) and Fiddler2 I kept making go requests directly to a JavaScript file and I'm 100% certain that it's pulling the file only from the cache at this point.

So I have three questions...

1.) Is using $_SERVER['SCRIPT_FILENAME'] the fastest way of having PHP pointing to the file?

2.) I'm using e for the server's time zone (local time zone on localhost and GMT on the live server); it seems to be working though is this acceptable?

3.) I noticed there were no ending semi-colons on other headers so I'm not certain what the correct/desired separator is for each header?

It seems to be working, I just want to be 1,000% sure that I've got everything covered and optimized as much can be even if it's only two lines of code. :)

- John

<?php
header('Cache-Control: max-age=604800;');
header('Last-Modified: '.date('D, d M Y H:i:s e.', filemtime($_SERVER['SCRIPT_FILENAME'])).';');
?>

httpwebwitch

2:27 pm on Dec 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) the $_SERVER array is created before the page/script is executed. it's already there, you just read from it. So, $_SERVER['SCRIPT_FILENAME'] is blazingly fast. No bottleneck there.

2) with regard to time zones, RFC2616 [w3.org] is explicit:
All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT),
without exception. For the purposes of HTTP, GMT is exactly equal to UTC
(Coordinated Universal Time). This is indicated in the first two formats by
the inclusion of "GMT" as the three-letter abbreviation for time zone, and
MUST be assumed when reading the asctime format. HTTP-date is case sensitive
and MUST NOT include additional LWS beyond that specifically included as SP in
the grammar.
source [w3.org]

so, try to make your date look like this: "Sun, 06 Nov 1994 08:49:37 GMT"

3) headers end with a newline. semicolons are used within headers to delimit optional parameters. ("\n" shown)
for example:

Content-Type: text/html; charset=iso-8859-1\n
Content-Disposition: attachment; filename=$fn; size=$size\n
Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT\n
Content-Length: 3495\n
\n

coopster

4:02 pm on Dec 14, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can use RFC 1123 format:
header('Last-Modified: ' . gmdate(DATE_RFC1123));

JAB Creations

10:19 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks to both of you for your replies! Glad I'm using the right variable to for the last modified date, I was using some PHP method before months ago and it was deathly slow.

I've been using GMT for months now. One of the troubles I've had was converting Unix time stamps (NOW() for MySQL generating something like 2010-12-15 01:00:00 in example) to 1123, still not sure if my date conversion function handles that correctly or not as I still have to eventually add complex last-modified header for more dynamic database driven content on my site (that's going to be fun, not!) however I've got scripts and style sheets caching just fine now which has been my primary concern. :)

- John