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?

3
  • "Yes, it isn't slow ", or "Yes, it is slow" ? =) (Sorry for my bad-understanding English) Commented Jun 23, 2011 at 21:49
  • yes it is slow to make yout php code parse and hash static files at each request Commented Jun 23, 2011 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. Commented Jun 23, 2011 at 21:56

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

filemtime() is the way I'd go, too.
So, it the Platinum Azure version, as I see. timestamp uses less resources than hashing...
Thanks for your answer. I accepted Platinum Azure's questions, because he was first =)
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.

4 Comments

It's not cruel. He's breaking the cache every time the file changes, which is perfect behavior.
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.
@SLaks - I'm breaking the cache every time the file changes. But I hash file every load of page. Isn't it cruel? =)
@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).
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.