【问题标题】:Prevent local caching of responses in HTTP streaming防止本地缓存 HTTP 流中的响应
【发布时间】:2012-04-16 12:48:47
【问题描述】:

我正在尝试将从文件读取的数据流式传输到 chrome 客户端。我能够成功传输数据,但我的响应被缓存了,我想防止这种情况发生。出现这种情况是因为我的平面文件包含彼此独立的数据条目,我想同样对待它们。例如,我的文件包含:

{idle_time:94125387364,system_time:98954710321,user_time:3683963615} {idle_time:94125387789,system_time:98954710456,user_time:3683963845} {idle_time:94125387876,system_time:98954710678,user_time:3683963986}

所以而不是得到 {idle_time:94125387876,system_time:98954710678,user_time:3683963986} (第三次进入) 作为 xmlhttprequest.responsetext,我收到了

{idle_time:94125387364,system_time:98954710321,user_time:3683963615} <br/>
{idle_time:94125387789,system_time:98954710456,user_time:3683963845} <br/>
{idle_time:94125387876,system_time:98954710678,user_time:3683963986}

注意:我不担心断线标签和空格。

我的 PHP 脚本如下所示, 测试.php

<?php
set_time_limit(0); 
$filename = 'D:\Smoke_Test\data.txt';

function flush2 (){
echo(str_repeat(' ',256));
// check that buffer is actually set before flushing
if (ob_get_length()){            
    @ob_flush();
    @flush();
    @ob_end_flush();
}    
@ob_start();
}

$file_last_modified_time = 0;

while(true) 
{
$modified_time = filemtime($filename);
$processor_info = "";
if ($file_last_modified_time < $modified_time)  
{
    header("Expires: Sun, 20 Jan 1985 00:00:00 GMT"); // date in the past
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    $file_last_modified_time = $modified_time;
    $handle = fopen($filename,"r");
    $processor_info = fgets ($handle);
    fclose ($handle);
    @ob_clean();
    echo $processor_info."<br/>";
    //flush2();
}
flush2();
sleep(1);
clearstatcache(true, $filename);
}

?>

我的 html 页面如下所示: 主页.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-     transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type="text/javascript" language = "javascript">
function read_file ()
{
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4 || xmlhttp.readyState==3 ) //&& xmlhttp.status==200)
        {
            handle_data (xmlhttp.responseText);
        }
      }
    xmlhttp.open("POST","test.php",true);
    xmlhttp.send();
}

function handle_data (input)
{
    document.getElementById("txtResponse").innerHTML=input;
}
</script>

</head>
<body>
<p>
<input type="button" id="dtnSendRequest" value="Send Request" onclick="read_file()"/>
</p>
<p>
response : <span id="txtResponse"></span>
<!-- <input type="text" id="txtResponse" width="500"/> -->
</p>
</body>
</html>

【问题讨论】:

    标签: php javascript apache caching xmlhttprequest


    【解决方案1】:

    尝试将其添加到您的 php 文件的顶部

    header("Expires: Sun, 20 Jan 1985 00:00:00 GMT"); // date in the past
    header("Cache-Control: no-cache");
    header("Pragma: no-cache"); 
    

    【讨论】:

    • 我更改了 php 代码,但问题仍然存在。您可以看到有问题的更改代码。
    • 请求页面时浏览器中的标题是什么意思?
    • 请求标头:POST /Dashboard/test.php HTTP/1.1 主机:localhost:8080 连接:keep-alive 内容长度:0 来源:localhost:8080 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19 Accept: / Referer: localhost:8080/Dashboard/Home.htm Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
    • 响应头:Cache-Control:no-cache Connection:Keep-Alive Content-Type:text/html Date:Sun, 15 Apr 2012 15:30:29 GMT Expires:Sun, 20 Jan 1985 00:00:00 GMT Keep-Alive:timeout=5, max=100 Pragma:no-cache Server:Apache/2.2.21 (Win64) PHP/5.3.10 Transfer-Encoding:chunked X-Powered-By:PHP/ 5.3.10
    【解决方案2】:

    使用它来强制无缓存:

    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
    

    确保在发送任何输出之前调用它。

    【讨论】:

    • 我更改了 php 代码,但问题仍然存在。您可以看到有问题的更改代码
    • 得到警告:无法修改标头信息 - 标头已发送(输出开始于 C:\wamp\www\Dashboard\test.php:14)
    【解决方案3】:

    你无法避免 responseText 发生的事情。它的 Http Streaming(不是本地或任何缓存)的性质使这种情况发生。我找到了一篇非常好的文章,其中说明了我的情况以及解决方案。

    文章是http://ajaxpatterns.org/archive/HTTP_Streaming.php

    以下段落包含我的情况以及解决方案。

    “XMLHttpRequest 的 responseText 属性始终包含从服务器中清除的内容,即使连接仍处于打开状态。因此浏览器可以运行定期检查,例如查看其长度是否发生了变化。但有一个问题, 就是说, 一旦刷新, 服务就不能撤消它的任何输出. 例如, 来自计时器服务的 responseText 字符串可能看起来像这样: "12:00:00 12:01:05 12:01:10" ,而理想情况下它只是“12:00:00”,然后只是“12:01:05”,然后只是“12:01:10”。解决方案是解析响应字符串并只查看最后一个值. 更准确地说,最后一个完整的值,因为文本可能以部分结果结尾。这种技术的一个例子就是这样工作的。为了便于解析,服务输出由特殊标记“@END”分隔的每条消息@"(XML 标记将是一种替代方法)。然后,可以运行正则表达式来获取最新消息,该消息后面必须跟该标记才能 en确定完成”

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2011-02-11
      • 1970-01-01
      • 2011-03-25
      相关资源
      最近更新 更多