防止HTTP流中的响应的本地缓存

时间:2012-04-15 15:13:23

标签: php javascript apache caching xmlhttprequest

我正在尝试将从文件读取的数据流式传输到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}

注意:我并不担心breakline标签和空白空间。

我的PHP脚本看起来像这样, test.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页面如下所示: Home.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>

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

使用此方法强制禁止缓存:

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

确保在发送任何输出之前调用此方法。

答案 2 :(得分:0)

你无法避免使用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确定它是完整的“