Throdne
Throdne

Reputation: 619

Caching data from ajax request for next 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

Upvotes: 0

Views: 93

Answers (1)

Wesley Smith
Wesley Smith

Reputation: 19561

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.

Upvotes: 1

Related Questions