Stop Ajax Caching?

Get help using Construct 2

Post » Mon Sep 24, 2012 10:57 am

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?
B
6
Posts: 13
Reputation: 443

Post » Mon Sep 24, 2012 11:59 am

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!
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Mon Sep 24, 2012 12:28 pm

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.Steerpike2012-09-24 12:28:34
B
6
Posts: 13
Reputation: 443

Post » Mon Sep 24, 2012 1:17 pm

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...
B
6
Posts: 13
Reputation: 443

Post » Mon Sep 24, 2012 1:40 pm

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.
B
17
S
9
G
7
Posts: 249
Reputation: 6,922

Post » Mon Sep 24, 2012 2:23 pm

[QUOTE=rfisher] 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.[/QUOTE]

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.Wastrel2012-09-24 14:25:24
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Mon Sep 24, 2012 2:44 pm

My question is: why would you read a value without updating first?
Image Image
B
161
S
48
G
90
Posts: 7,350
Reputation: 66,755

Post » Mon Sep 24, 2012 2:46 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,574

Post » Mon Sep 24, 2012 3:12 pm

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.

[QUOTE=newt] My question is: why would you read a value without updating first?[/QUOTE]

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...)
B
6
Posts: 13
Reputation: 443


Return to How do I....?

Who is online

Users browsing this forum: gregorysforster, heliogame and 14 guests