hash css和js文件来破解缓存.它慢吗?

Lar*_*bar 2 php hash md5-file

我有一些生成页面模板的脚本.此外,此脚本在HTML中呈现<script><link rel='stylesheet'>标记.

我想用"?v = xxxxx"参数添加缓存中断功能.

我是这样做的:

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
Run Code Online (Sandbox Code Playgroud)

每次用户刷新页面时,要散列大约十几个文件,这不是很慢吗?

cyp*_*her 5

就个人而言,我不会散列文件,这是浪费资源.而不是它,我会将最后修改的时间戳添加到v?= ....我的意思是这样的:

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
Run Code Online (Sandbox Code Playgroud)