缓存有问题...
我使用这个带有 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 小时的缓存