散列css和js文件以破坏缓存慢吗?

问题描述

| 我有一些脚本可以生成页面模板。另外,此脚本在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
每次用户刷新页面时要散列大约十二个文件不是很慢吗?     

解决方法

        这对您的用户每次破坏缓存都是残酷的。您多久更改一次这些文件? 无论如何,我建议使用时间戳-比md5快得多。     ,        就个人而言,我不会对文件进行哈希处理,那是浪费资源。取而代之的是,我将最后修改的时间戳添加到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
    ,        根据您更新网站的方式,您可能应该改用修改日期。 但是,如果您总是重新上传每个文件,则不是一个好主意。 但是,随后您应该能够将哈希值缓存在内存中(也许还可以检查时间戳记)