假如说有一个a.php文件,这个程序是采集其他网站的信息,当用户访问网页的时候会用ajax触发这个php程序进行采集,但是我不想让用户每次来的时候都进行采集,规定1个小时内的内容不变,一个小时后才进行采集,这个功能怎么实现?要进行缓存么?缓存文件用什么格式的?文件内的内容该怎么写?求大神们给个思路!

解决方案 »

  1.   

    问题在于你如何知道内容没有改变?
    如果目标是纯静态页面,是可以用 get_headers 获取最后修改时间来判断一下
    如果目标是动态页面,你不取回来比较,就不知道是否改变了
      

  2.   

    <!DOCTYPE html>
    <body>
    <div id="msg"></div>
    <script>
    var xmlhttp;
    if(window.ActiveXObject)
    {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
    xmlhttp=new XMLHttpRequest();
    }
    xmlhttp.open("POST", "a.php", true);  
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
        xmlhttp.send("data=1");     
        xmlhttp.onreadystatechange = function(){  
            if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
    document.getElementById("msg").innerHTML=xmlhttp.responseText;
     }     
        }
    </script>
    </body>
    </html><?php
    $time=time();
    $time2=file_get_contents("time.dat");
    if($time-3600>$time2){
    file_put_contents("time.dat",$time);
    $con=file_get_contents("http://www.baidu.com");
    file_put_contents("content.dat",$con);
    }else{
    $con=file_get_contents("content.dat");
    }
    echo $con;
    ?>
      

  3.   

    借助liux的 crontab
    或者生成一个锁文件 判断这个文件生成时间与当前时间是否相距1小时了