散列css和js文件来破解缓存。它慢吗? [英] hash css and js files to break cache. Is it slow?

查看:105
本文介绍了散列css和js文件来破解缓存。它慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



我这样做:

  foreach($ scripts as& $ script){

//获取脚本文件名
$ script ={$ this-> _js_folder} / {$ script};

//得到它的真实路径
$ realfile = realpath(substr($ script,1));

//散列文件
$ hash = md5_file($ realfile);

//添加缓存破解号
$ script。='?v ='。$ hash;
$ b $} //:foreach

不是很慢,每次用户刷新页面时都会打开几十个文件?解析方案

这对您的用户每次都会破坏缓存很残酷。你多久换一次这些文件?



无论如何,我会建议使用时间戳 - 远比md5快。


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?

解决方案

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.

这篇关于散列css和js文件来破解缓存。它慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆