Stop Ajax Caching?

0 favourites
  • 10 posts
  • I'm making a multiplayer card game.

    It uses Ajax to make PHP/SQL calls to a database.

    It all works perfectly except for one BIG thing:

    Ajax is caching results - so sometimes I get the state of the database as it was 5 or 10 minutes ago.

    I've tried making the Request call unique by adding the time or tickcount to it. But every so often that time or tickcount matches one that's been seen on a previous run, I get the cached result and it all falls apart.

    I could try adding more random numbers to the Request, but there's always the chance that it'll match one already seen.

    There has to be a less hacky way of stopping Ajax caching results (I've seen there's an ajaxSetup command to turn off the cache, but I have no idea where that would go in the files that Construct 2 creates)

    It's driving me mad - I'm at my wit's end...

    Please can someone help?

  • Well, I'm not familiar with the technical AJAX stuff, so here's a simple (yet hacky) solution in case a smarter person doesn't respond later: add a variable that increments every time you send a request. Boom! Impossible to get the same value twice! <img src="smileys/smiley14.gif" border="0" align="middle" />

  • I already do a similar thing by adding tickcount to the request.

    The trouble is that when you restart the application (something I'm doing every few minutes when I'm developing) then I'm picking up data from previous runs - and the incrementing variable/tickcount doesn't fix this.

  • Ah. I've just had the problem occur on someone else's machine the first time they ran the program.

    Which I guess means it must be a server-side database caching problem rather than a client-side Ajax problem?

    I'd still really appreciate some help if anyone has any ideas how to solve this...

  • The javascript function getTime() returns the number of milliseconds since midnight Jan 1, 1970 and will always be unique. If you can use this variable it will solve your problem. The problem is how to implement this solution.

  • The javascript function getTime() returns the number of milliseconds since midnight Jan 1, 1970 and will always be unique. If you can use this variable it will solve your problem. The problem is how to implement this solution.

    In case this is the way you want to go, check out this plugin: http://www.scirra.com/forum/plugin-system-date-and-time_topic45206.html

    Specifically, the UnixTimestamp expression will give you the number rfisher is talking about.

  • My question is: why would you read a value without updating first?

  • The typical way is to add a timestamp (time in seconds since 1970) to the request URL, e.g. myfile.php?t=187945933...

    Unfortunately C2 doesn't give you a way to get this yet - a third party plugin can help though.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks everyone for the help so far - that defnitely looks like the way to avoid getting stale Ajax data when I re-run.

    However, I think now that the problem may be database-side rather than client-side -I'm having similar problems on machines that have never run the program before.

    My question is: why would you read a value without updating first?

    I'm not sure I understand your question. Mostly I'm doing repeated reads on the database to see if any new plays have been made. Sometimes that will be after I've made a play myself, but often it's just waiting for a new entry in the Play table. The trouble is that I can go into myPHPAdmin and delete plays, and they still show up in my Ajax requests minutes or even hours later...)

  • Sorry for replying on such an old topic, but since this describes my problem perfectly, I couldn't resit

    Is there a fix for this kind of behavior? When I'm testing my game I'm constantly refreshing the webpage to see if a new SQL query or a new ajax request is working like it should be. Be I'm constantly getting back old data in my browser while the data in the SQL database is updated to the last values.

    Since this is quite annoying for testing reasons, is there a way to prevent this?

    Any ideas? Or is the time thingy the only acceptable solution?

    Steerpike : If you are still active here, have you found a solution for this problem?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)