2

I have some script that generates templates of a page. Also, this scripts renders <script> and <link rel='stylesheet'> tags in the HTML.

I'd like to add cache-breaking feature with "?v=xxxxx" parameter.

I do it in such a way:

foreach ($scripts as &$script) {

    // get script file name
    $script = "{$this->_js_folder}/{$script}";

    // get it's realpath
    $realfile = realpath(substr($script,1));

    // hashing the file
    $hash = md5_file($realfile);

    // adding cache-breaking number
    $script .= '?v='.$hash;

} //: foreach

Isn't it slow, to hash about a dozen files every time user refreshes the page?

Larry Foobar
  • 11,092
  • 15
  • 56
  • 89
  • "Yes, it isn't slow ", or "Yes, it is slow" ? =) (Sorry for my bad-understanding English) – Larry Foobar Jun 23 '11 at 21:49
  • yes it is slow to make yout php code parse and hash static files at each request – regilero Jun 23 '11 at 21:51
  • Why would you do this? Browsers/Servers already cooperate to only transfer data if it was modified since the last retrieval. That's what the If-Modified-Since header and 304 status codes are for. – Marc B Jun 23 '11 at 21:56

3 Answers3

5

Personally, I wouldn't hash the file, that's a waste of resources. Instead of it, i would add the last-modified timestamp into the v?=.... I mean something like this:

foreach ($scripts as &$script) {

    // get script file name
    $script = "{$this->_js_folder}/{$script}";

    // get it's realpath
    $realfile = realpath(substr($script,1));

    // getting last modified timestamp
    $timestamp = filemtime($realfile);

    // adding cache-breaking number
    $script .= '?v='.$timestamp;

} //: foreach
Larry Foobar
  • 11,092
  • 15
  • 56
  • 89
cypher
  • 6,822
  • 4
  • 31
  • 48
3

That's cruel to your users to break the cache every time. How often do you change those files?

At any rate, I would suggest using a timestamp-- far faster than md5.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 7
    It's not cruel. He's breaking the cache every time the file changes, which is perfect behavior. – SLaks Jun 23 '11 at 21:49
  • 1
    I don't think he's suggesting breaking cache every time, but using the md5 checksum of the file so that it will break if the file is changed. – Problematic Jun 23 '11 at 21:50
  • @SLaks - I'm breaking the cache every time the file changes. But I hash file every load of page. Isn't it cruel? =) – Larry Foobar Jun 23 '11 at 21:51
  • @SLaks: Sorry. When I hear "cache-breaking" it usually means to try to make the page effectively uncacheable (or at least completely pointless to cache). – Platinum Azure Jun 24 '11 at 14:45
2

Depending on how you update your site, you should probably use the date modified instead.

However, if you always re-upload every file, this is not a good idea.
However, you should then be able to cache the hash in memory (and perhaps also check the timestamp)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964