8

I'm writing an AJAX based auto-complete and am trying to keep my server from being inundated with requests by using client side caching. The setup is as follows, Django serving JSON responses on the back-end, with the client using jQuery's AJAX routines to GET the appropriate data.

request

Looking at the response headers in Chrome, everything looks alright. The Cache-Control header is present, so I'm pretty sure that the problem is outside of Django's scope.

response

However, the server logs show that there was a request made to the API.

logs

Code:

This is the code I'm using to query the API.

function get(url, callback) {
    jQuery.ajax({
        type: 'GET',
        contentType: 'application/json',
        dataType: 'json',
        cache: true,
        url: url,
        success: callback
    });
}

Misc:

The little disable cache check-box isn't ticked, so Chrome should be caching responses.

Edit:

This is all set up in my development environment. So the server is serving from 127.0.0.1:8000.

rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • 1
    I've specified `Cache-Control: max-age=300, public, must-revalidate` in a tiny script and it caches fine. A minor note: make sure you're opening a page via clicking a link, otherwise (F5 or entering address) chrome specified `max-age=0` explicitly. – zerkms Jul 22 '14 at 02:03
  • After further investigation, that's exactly what was happening. – rectangletangle Jul 22 '14 at 04:55

1 Answers1

8

Chrome sends Cache-Control:max-age=0 in case if you open a page using address bar or F5.

That's it - if you want to see it working go to that page by a link click.

Related but obsolete answer: https://stackoverflow.com/a/385491/251311

Community
  • 1
  • 1
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Cool, going to the page via link seemed to return a cached response like I was expecting. This is one of the more request intensive parts of the API, so every little bit helps. Thanks! – rectangletangle Jul 22 '14 at 02:15
  • I know this is an old topic, but if the OP's intention (and also mine) is to create a REST API for a localhost server or any other server to query from, how can we protect the API being flooded of requests if the API is not clickable? A HTTP form or javascript will have the same effect as clicking a link? – dsenese May 31 '22 at 13:44