Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...