缓存 css 和 javascript
我在缓存方面遇到了麻烦...
我使用这个带有 url 重写的 php 文件来压缩和缓存 css 和 js
我的印象是,如果我更改/更新了我的一个文件,浏览器将检索更新的文件。但除非我清除缓存或刷新页面,否则它不会。
我的编码错误吗?或者浏览器在缓存过期之前不会获取更新的内容?
<?php
$file = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['file'];
$last_modified_time = filemtime($file);
$etag = md5_file($file);
$expires = 60*60*24*7;
if(file_exists($file))
{
if($_SERVER['HTTP_IF_NONE_MATCH'] != $etag)
{
header("Pragma: public");
header("Cache-Control: maxage=$expires, must-revalidate");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: \"{$etag}\"");
if($_GET['type'] == 'js') header('Content-type: application/javascript');
if($_GET['type'] == 'css') header('Content-type: text/css');
if($_GET['type'] == 'ico') header('Content-type: image/x-icon');
ob_start("ob_gzhandler");
include($file);
}
else {
header('HTTP/1.0 304 Not Modified');
}
}
else {
header("HTTP/1.0 404 Not Found");
}
?>
重写规则
RewriteRule ^(.*).js$ /compress.php?file=$1.js&type=js [L,QSA]
RewriteRule ^(.*).css$ /compress.php?file=$1.css&type=css [L,QSA]
RewriteRule ^(.*).ico$ /compress.php?file=$1.ico&type=ico [L,QSA]
---------
编辑:也许我应该以不同的方式来做这件事?大公司使用什么来缓存,以及他们如何强制浏览器在缓存设置过期之前获取更新的内容?
编辑2:谢谢家伙的帮助。我将使用 1 小时缓存
Im having trouble with caching...
Im using this php file with url rewriting to compress and cache css and js
I was under the impression that if i changed/updated one of my files, that the browser would retrieve the updated file. But it doesnt unless i clear the cache or refresh the page.
Is my coding wrong? Or is the browser not suppose to get updated content until the cache expiration period has expired?
<?php
$file = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['file'];
$last_modified_time = filemtime($file);
$etag = md5_file($file);
$expires = 60*60*24*7;
if(file_exists($file))
{
if($_SERVER['HTTP_IF_NONE_MATCH'] != $etag)
{
header("Pragma: public");
header("Cache-Control: maxage=$expires, must-revalidate");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: \"{$etag}\"");
if($_GET['type'] == 'js') header('Content-type: application/javascript');
if($_GET['type'] == 'css') header('Content-type: text/css');
if($_GET['type'] == 'ico') header('Content-type: image/x-icon');
ob_start("ob_gzhandler");
include($file);
}
else {
header('HTTP/1.0 304 Not Modified');
}
}
else {
header("HTTP/1.0 404 Not Found");
}
?>
rewrite rules
RewriteRule ^(.*).js$ /compress.php?file=$1.js&type=js [L,QSA]
RewriteRule ^(.*).css$ /compress.php?file=$1.css&type=css [L,QSA]
RewriteRule ^(.*).ico$ /compress.php?file=$1.ico&type=ico [L,QSA]
---------
EDIT: Maybe i should be doing this a different way? what do the big companys use to cache, and how do they force browsers to get the updated content before the cache is set to expire?
EDIT 2: Thanks guy for the help. Im going with a 1 hour cache
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在给定的
Expires
标头过期之前,浏览器不会刷新缓存文件。如果过期,它将请求带有If-None-Match
标头的文件(我猜)。但为什么不通过
.htaccess
处理缓存控制呢?您可以检查mod_expires
:Gzip 压缩为与
mod_deflate
配合良好:编辑:'Big公司不使用
Expires
或max-age
标头,或者他们将设置这些标头以让文件缓存约 1 小时 ->与缓存的冲突将被最小化。您将其设置为 1 周。The Browser doesn't refresh cached files until the given
Expires
header expired. If it's expired, it will request the file with anIf-None-Match
header (I guess).But why didn't you handle cache control via
.htaccess
? You could checkmod_expires
:Gzip compression as well with
mod_deflate
:Edit: 'Big companies' don't use
Expires
ormax-age
headers, or they will set these headers to let cache files for ~1 hour -> conflicts with caching will be minimized. You're setting it to 1 week.我相信您错过了这些部分...
顺便说一句,为了帮助您确定更多与您的缓存性能甚至整个应用程序性能相关的信息,您可以使用 Google API 性能或这些网站来测试它:< a href="http://www.webpagetest.org/" rel="nofollow">http://www.webpagetest.org/ (PS:仅举个例子,这是我的最新结果工作博客:http://www.webpagetest.org/result/110803_SB_17PVH/)
You missing these part, i believe...
btw, for helping you determine, much more, related how your cache performance, or even better, your whole app performace, you can test it by using Google API performance, or these sites : http://www.webpagetest.org/ (PS: just for example, these is my latest result for my on working blog : http://www.webpagetest.org/result/110803_SB_17PVH/)
是的,理论上浏览器应该注意您发回的
Cache-Control
、Expires
等信息,但实际上,这并不总是一个好主意相信浏览器会做正确的事情。您可能需要考虑做的是在 compress.php 脚本中添加第二步...让它重定向到实际的压缩文件,并附加类似
"?ts=".$last_modified_time
到文件的路径。这样,当文件更改时,URL 也会更改,并且浏览器更有可能执行正确的操作并获取最新的文件。我以前使用过类似的技术。Yes, in theory the browser should pay attention to the
Cache-Control
,Expires
, etc. information you're sending back, but in practice it's not always a good idea to trust the browser to do the right thing.What you might want to consider doing is adding a second step in your compress.php script ... have it do a redirect to the actual compressed file, and append something like
"?ts=".$last_modified_time
to the path to the file. That way the URL will change when the file changes, and the browser will be much more likely to do the right thing and get the most recent file. I've used a similar technique before.