0

In my code I have a bunch of ajax requests that have the cache option set to true.

When I run my code, how can I tell whether the data was retrieved from cache or retrieved from the server (first request, or cache expired, or cache bypassed for some reason)?

Ideally, I'd like to do it within the JavaScript code itself, as I might have several calls running in parallel.

Christophe
  • 27,383
  • 28
  • 97
  • 140

4 Answers4

3

In the chrome dev tools, in the network tab you can see all the requests and in the size column it will say (from cache) if it was retrieved from the cache.

dezman
  • 18,087
  • 10
  • 53
  • 91
0

Check this out: https://code.google.com/p/ajax-cache/

Please be aware that cache:true only works with GET and HEAD requests.

My Stack Overfloweth
  • 4,729
  • 4
  • 25
  • 42
0

Cache is managed by the browser (out of your direct control), so it's kind of unpredictable.

If parameter are exactly the same, cache should be used.

If you want to make sure you Ajax is always "a fresh call", add some parameter with for example the current timestamp to you Ajax request.

....

    if (!args.url.indexOf("?")) args.url=args.url+"?xxxtmstmp="+new Date().getTime();
    else args.url=args.url+"&xxxtmstmp="+new Date().getTime();
...
Cedric Simon
  • 4,571
  • 4
  • 40
  • 52
0

Check 304 not modified status is to see the response from ajax. If the response value is not changed, then it gives 304 not modified. You can store the response value locally and check with the new response.

As stated Here:

“notmodified”

This code indicates that the request completed normally but that the server sent an HTTP 304 “Not Modified” response, indicating that the requested URL has not changed since it was last requested. This status code only occurs if you set the ifModified option to true. (See Common Options.) jQuery 1.4 considers a “notmodified” status code a success, but earlier versions consider it an error.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • This is specific to jQuery, it would be interesting to learn how they get the "notmodified" status (it's certainly not from the status code). The answer might be in the post quoted in my own question. – Christophe Oct 04 '13 at 21:20