一般浏览器请求php是不会被缓存的,除非php直接显示的发送head 304
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$browserCachedCopyTimestamp = strtotime(preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']));
if($browserCachedCopyTimestamp + 604800 > time()){
header("http/1.1 304 Not Modified");
header ("Expires: " . gmdate ("r", (time() + 604800)));
header ("Cache-Control: max-age=604800");
exit;
}
}
$id=I('get.id',0,'intval');
$file = UPLOAD_IMG_PATH . 'qccode/' . $id . '.png';
if(file_exists($file)){
header ("Last-Modified: " . gmdate ('r', time()));
header ("Expires: " . gmdate ("r", (time() + 604800)));
header ("Cache-Control: max-age=604800");
header ("Pragma: public");
Header ("Content-type: image/png");
echo file_get_contents($file);
}else{
$qr = \APPlib\get_app_lib('phpqrcode',false);
$site = C('SITE_URL');
$value=$site . $id . '.html';
$errorCorrectionLevel = "L";
$matrixPointSize = "4.5";
\QRcode::png($value, $file, $errorCorrectionLevel, $matrixPointSize);
echo file_get_contents($file);
}
上面就是通过304来缓存一个动态的js,根据文件的修改时间来判断,这里需要注意一点,如果再次之前有session_start,要注意session_cache_limiter,他会发送一个不缓存的头信息。
这种情况需要可以强制发送一个program public的头来规避,注意在发送304的时候最好还是补充一下缓存头,我在缓存js时可以不在重设,可能是由于我是通过服务器重定向模拟的动态js吧(请求路径是js,但是是动态生成的)。