0

I have an odd problem which I am trying to make solve now for a while. I have made a web app that gets data for a graph which is rendered using Highcharts.

The script fetches data from the DB, saves it into a file and renders the graph using $.getJson method in jquery which goes to another ajax file and processes the data that was saved. The user can spend x amount of time browsing the graph, and everytime the user changes the zoom level, an ajax request is sent back to my ajax page which again uses the saved data file to send graph data back.

The problem now is that if another user uses the app whilst the original user is still using the graph and its constantly being updated by the ajax file, the saved data file is overwritten and the original user will have the wrong graph data sent back to them.

I considered using $_SESSION however the data can be very large at times. Any other ideas?

GSinghLDN
  • 161
  • 8
  • 2
    You may call me a meddler but... why is an intermediate file needed here? I mean, if you just want to show a single state of the chart in a given time lapse, why not to make the Ajax requests in that defined interval to a server side script that recalculates the data on the fly? – Carlos Dec 04 '13 at 15:03
  • I'd have to agree with jackflash here, i see no reason for your intermediary file. There is no reason the original user should have to re-request the data on zoom if the data isn't changing, and even if the data were changing, how expensive is it to just get the data again from the database? – Kevin B Dec 04 '13 at 15:11
  • It is very expensive to get the data from the db... as it takes a lot of time for it to be fetched and processed. Also, Highcharts struggles with a large amount of data which is the reason why I need to use an intermediate file so the data can be averaged out to less points which highcharts can handle. – GSinghLDN Dec 04 '13 at 15:25
  • Right.. but every time you request the intermediate file you're getting the entire file, then filtering it with javascript right? As far as the database query, have you optimized the table and the query? – Kevin B Dec 04 '13 at 15:28
  • No, php is filtering it out and then sending the data back using a json encoded array which is then being iterated over at client side by javascript and pushed in the graph. Unfortuantely, the table isn't in the best shape and indexed quite bad, this is something I have no control over to get it changed which is why I have to make do. – GSinghLDN Dec 04 '13 at 15:38
  • Understood. Would it be a possibility to build temporary in-memory lookup tables that would make querying this huge table that is indexed badly easier? If not then i can see why you're using temporary files instead. using the session_id would be the best bet to keep the files from overlapping. – Kevin B Dec 04 '13 at 15:41
  • Why not change the query to do the point limiting? Without seeing the SQL schema and query it is hard to tell here what the reasoning is behind the intermediate file. We have procs that hit tables JOINed to other tables in the hundreds of millions of records (well indexed of course) and have no issue filtering out based on the params in the proc. – wergeld Dec 04 '13 at 15:51

2 Answers2

3

If you've got multiple users, then don't use the same filename for all the users. e.g.

$cache_file = 'cache-' . session_id() . '.txt';

so that the file becomes tied to the particular user.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Is it possible for the files to be deleted automatically after a user is finished with it as this way would cause a huge amount of files being built up over time. – GSinghLDN Dec 04 '13 at 14:58
  • 1
    no. that'll be up to you. If you roll your own session handler, you could use the session cleanup routine to delete these cache files as well. Otherwise a simple cron job to delete files that haven't been touched in a certain period would do the trick as well. – Marc B Dec 04 '13 at 14:58
1

You could use different files for different users.

If you add the value of session_id() to your filename you'll have different files for every working session.

Oscar Pérez
  • 4,377
  • 1
  • 17
  • 36