dongye9820 2016-03-04 04:33
浏览 47
已采纳

从ajax请求缓存数据以获取下一个ajax pull

I wanted to know what's the best way about going around my issue...

So, I want to have a live chart that is updating every second. I currently have a python script running on a raspberry pi that adds a new entry of data to a MySQL server every second. I'm working on a website that will nicely display this info using Ajax to refresh the data (I currently have Ajax pulling the only latest entry). I will have a 2 minute (120 seconds) line graph that will changes every second. Dropping the last second (second 120) and added a new entry to the timeline at second 1.

My question is, What's the best way to cache seconds 1-119 without having to make a MySQL query for the latest 120 entries every second. I figured caching it some way would be less data for the MySQL server to gather and push, and less network traffic for the Ajax to request.

Thanks for your replies, Throdne

  • 写回答

1条回答 默认 最新

  • donglingsai2880 2016-03-04 04:46
    关注

    Something like this should work:

    <?php
    $_SESSION['results']=['red','blue','green'];
    
    $newRow='black'; // some new value to add
    array_shift ($_SESSION['results'] ); // remove the value at index 0
    $_SESSION['results'][]=$newRow; // add the new value at the end
    
    var_dump($_SESSION['results']);
    ?>
    

    Where $_SESSION['results'] is the full results from the first query and $newRow; is the result from an new query for just the one new row –

    However, doing this every second may get messy. Especially since you'll likely use ajax to refresh the results and the timing may be tough to manage

    Since it seems that you are wanting to stream data, I dont have any experience with them myself, but it sounds like you might find Web Workers useful for this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?