Search code examples
phphashmd5-file

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


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?


Solution

  • 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.