hash css and js files to break cache. Is it slow?
Asked Answered
S

3

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?

Shimmy answered 23/6, 2011 at 21:45 Comment(3)
"Yes, it isn't slow ", or "Yes, it is slow" ? =) (Sorry for my bad-understanding English)Shimmy
yes it is slow to make yout php code parse and hash static files at each requestLoss
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.Lithuanian
T
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.

Tolley answered 23/6, 2011 at 21:46 Comment(4)
It's not cruel. He's breaking the cache every time the file changes, which is perfect behavior.Incredible
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.Hesitation
@Incredible - I'm breaking the cache every time the file changes. But I hash file every load of page. Isn't it cruel? =)Shimmy
@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).Tolley
T
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
Tick answered 23/6, 2011 at 21:48 Comment(3)
filemtime() is the way I'd go, too.Hesitation
So, it the Platinum Azure version, as I see. timestamp uses less resources than hashing...Shimmy
Thanks for your answer. I accepted Platinum Azure's questions, because he was first =)Shimmy
T
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.

Tolley answered 23/6, 2011 at 21:46 Comment(4)
It's not cruel. He's breaking the cache every time the file changes, which is perfect behavior.Incredible
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.Hesitation
@Incredible - I'm breaking the cache every time the file changes. But I hash file every load of page. Isn't it cruel? =)Shimmy
@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).Tolley
I
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)

Incredible answered 23/6, 2011 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.