http cache 原理实战演习
有篇博文介绍的原理已经比较清楚了,见下面链接, 本文给出实验结果。
http://www.cnblogs.com/cocowool/archive/2011/08/22/2149929.html
Last-modified
第一次请求,请求头中没有 IF_Modify_Since, 因为第一次还没有获得 Last-Modified时间值, 本次请求只能是无条件请求。
GET http://localhost/httpcache.php HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en-GB;q=0.6,en;q=0.4
第一次响应, 响应头有 Last-modified值, 浏览器缓存此值,待下次网页访问执行有条件访问, 即将此值作为请求报文头中 If_Modify_Since值。
HTTP/1.1 200 OK
Date: Mon, 16 Jun 2014 16:12:13 GMT
Server: Apache/2.4.7 (Win32) OpenSSL/0.9.8y PHP/5.4.25
X-Powered-By: PHP/5.4.25
Last-modified: Fri, 16 Mar 2007 04:00:25 GMT
Content-Length: 524
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style></style>
</head>
<body>
<h1>hello world!11</h1>
<input type="text" value="test"/>
<input type="button" value="button"/>
<script type='text/javascript'>
$("[type='button']").click(function(){
$("[type='text']").val("button click");
})
</script>
</body>
</html>
第二次浏览器请求, 带有If-Modified-Since
GET http://localhost/httpcache.php HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en-GB;q=0.6,en;q=0.4
If-Modified-Since: Fri, 16 Mar 2007 04:00:25 GMT
第二次响应, 带有304状态,并没有报文体, 则告诉浏览器使用上次缓存。
HTTP/1.1 304 Not Modified
Date: Mon, 16 Jun 2014 16:23:40 GMT
Server: Apache/2.4.7 (Win32) OpenSSL/0.9.8y PHP/5.4.25
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
代码:
<?php
$if_mod_since = ""; $headers = getallheaders();
while (list($header, $value) = each($headers)) {
if ( "If-Modified-Since" == $header )
{
$if_mod_since = $value;
break;
}
} if ( $if_mod_since != "" )
{
//告诉浏览器文档内容没有发生改变
header('HTTP/1.1 304 Not Modified');
exit(0);
}
else
{
header("Last-modified: Fri, 16 Mar 2007 04:00:25 GMT");
}
?>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style> </style>
</head>
<body>
<h1>hello world!11</h1>
<input type="text" value="test"/>
<input type="button" value="button"/>
<script type='text/javascript'>
$("[type='button']").click(function(){
$("[type='text']").val("button click");
})
</script>
</body>
</html>
ETAG
与 第一个 类似, 给出代码可运行演示。
<?php
$if_none_match = ""; $headers = getallheaders();
while (list($header, $value) = each($headers)) {
if ( "If-None-Match" == $header )
{
$if_none_match = $value;
break;
}
} if ( $if_none_match == "ead145f" )
{
//告诉浏览器文档内容没有发生改变
header('HTTP/1.1 304 Not Modified');
exit(0);
}
else
{
header("ETag: ead145f");
}
?>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style> </style>
</head>
<body>
<h1>hello world!11</h1>
<input type="text" value="test"/>
<input type="button" value="button"/>
<script type='text/javascript'>
$("[type='button']").click(function(){
$("[type='text']").val("button click");
})
</script>
</body>
</html>
Expires 和 Max-age 支持浏览器离线缓存有效期定制,即不访问服务器
给出连个php头,可实验验证, 通过chrome浏览器前进后退访问, 无http请求, 仍然可显示页面,
但是URL地址栏回车键, 或者F5都会引起http重新请求。
header("Max-age: 86400");
header("Expires: Tue, 20 Mar 2015 04:00:25 GMT");
最新文章
- Arduino uno 教程~持续更新~
- MyBabis 用法详解
- 【vscode】如何在vscode 中配置:TypeScript开发node环境
- js的querySelector跟querySelectorAll
- Direct3D11学习:(八)Effects介绍
- AngularJS-入门篇
- python ImportError: No module named 的问题
- 如何修改Ubuntu双系统的默认开机启动项
- 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
- php配置redis支持
- linux学习之四---gdb调试
- go语言实现遍历目录,及查找特定的文件类型
- [每日一题] OCP1z0-047 :2013-08-01 正则表达式--- REGEXP_REPLACE 函数
- 将WebApi Host到控制台和IIS
- java克隆之深拷贝与浅拷贝
- MFC误报内存泄露的修复
- mac环境下jdk配置
- 【转】协同开发中SVN使用规范试用
- poj3924
- KOL:Key Opinion Leader
热门文章
- Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guy
- [ACM训练] DEV C++如何处理不能调试
- 为什么我们要使用min-height和max-height样式属性?
- URAL 1287. Mars Canals
- ThinkPHP3.2.3--Linux服务器首页文件index.php路径配置问题
- C# Textbox的ImeMode取值对中文输入法的影响 (转)
- Java面向对象编程
- orcale 动态执行语句
- JAVA中保留指定小数位方法
- markdown语法学习笔记