gpt4 book ai didi

php - 散列 css 和 js 文件以打破缓存。慢吗?

转载 作者:行者123 更新时间:2023-12-02 06:22:50 25 4
gpt4 key购买 nike

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

我想使用“?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

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

最佳答案

就我个人而言,我不会对文件进行哈希处理,那是一种资源浪费。相反,我会将最后修改的时间戳添加到 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

关于php - 散列 css 和 js 文件以打破缓存。慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6460967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com