利用http缓存数据
2016-07-07 20:27
357 查看
通过一个简单的ajax请求来详解http的缓存技术
register.html
UserCheck.php
php中通过
$expire=604800;
header('Cache-Control: max-age='.$expire);//1 month
将缓存设置为一个月,则当进行相同的数据请求时,返回的只会从缓存中取
由上图可得当进行第二次相同的请求时,数据就会从缓存中取
改变UserCheck.php代码如下
在header中禁用缓存
由图片可得每一次请求都会从新从后台获取数据。
加载html页面时,浏览器会自动缓存css,img,html等文件,强制刷新才会从新加载,可以使用一下代码禁用缓存
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
但不一定有效。。。
缓存技术不一定好,平衡点??
register.html
<!DOCTYPE> <html> <head> <title>http缓存</title> <script type="text/javascript"> window.onload = function() { var http_request; var oBtn = document.getElementById('btn'); oBtn.onclick = function() { sendRequest(); } } function sendRequest(){ var u=document.getElementById("username").value; if(window.ActiveXObject){ http_request=new ActiveXObject("Microsoft.XMLHTTP"); }else{ http_request=new XMLHttpRequest(); } if(http_request){ var url="UserCheck.php?username="+u; http_request.open("GET",url,true); http_request.onreadystatechange=chuli; http_request.send(); } } function chuli(){ if(http_request.readyState==4){ if(http_request.status==200){ var res=http_request.responseText; console.log(res); } } } </script> </head> <body> <form action="" method=""> 用户名字:<input type="text" name="username" id="username"><input type="button" id="btn" value="验证用户名"> </form> </body> </html>
UserCheck.php
<?php $expire=604800; header('Cache-Control: max-age='.$expire);//1 month $username=$_REQUEST['username']; if($username=="pcd"){ echo "err"; }else{ echo "ok"; } ?>
php中通过
$expire=604800;
header('Cache-Control: max-age='.$expire);//1 month
将缓存设置为一个月,则当进行相同的数据请求时,返回的只会从缓存中取
由上图可得当进行第二次相同的请求时,数据就会从缓存中取
改变UserCheck.php代码如下
<?php header("Expire: -1"); header("Cache-Control: no-cache"); header("Pragma: no-cache");//三个均代表禁用缓存,兼容不同的浏览器 $username=$_REQUEST['username']; if($username=="pcd"){ echo "err"; }else{ echo "ok"; } ?>
在header中禁用缓存
由图片可得每一次请求都会从新从后台获取数据。
加载html页面时,浏览器会自动缓存css,img,html等文件,强制刷新才会从新加载,可以使用一下代码禁用缓存
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
但不一定有效。。。
缓存技术不一定好,平衡点??
相关文章推荐
- 【Netty4.X】TCP粘包/拆包问题的解决办法(二)
- Linux网络编程
- http://www.ibm.com/developerworks/cn/java/j-lo-junit-src/
- http://my.oschina.net/pangyangyang/blog/144495
- Apache 部署https证书
- http://codeforces.com/gym/100803/attachments A题 Bit String Reordering
- 雨水泛洪和网络泛洪那个更可怕
- 雨水泛洪和网络泛洪那个更可怕
- okhttp3的简单使用
- 网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life
- iOS开发 - 第04篇 - 网络 - 02 - JSON解析 & 请求 & 黑酷例子 & HTTP通信
- 使用ImageNet在faster-rcnn上训练自己的分类网络
- HTTP深入浅出个人总结
- HTTP深入浅出个人总结
- 网络爬虫技术
- 一步一步演示神经网络回馈算法过程
- [机器学习笔记]Note8--机器学习应用建议
- APP开发流程实例讲解-儒释道网络电台八天开发全程-优化排错:增强稳定性和添加异常处理
- Http 分块传输编码 Transfer-Encoding: chunked
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2