1

I do not want to over the limit of requests I have in my Google Maps membership. Often the requests are the same so I don't need to re-request them.

If I am doing this:

$.ajax({
  type: "GET",
  headers: {
    "Content-Type": "application/json"
  },
  cache: true,
  url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?INPUT_AND_API_KEY_AND_SOME_OTHER_PARAMS",
  data: JSON.stringify(myData),
  success: function(response) {
    //code
  }
});

Will it be enough?

When I say be enough, I mean: do not requesting the same data based on previous input data, OR I should do something more complex.

Thanks

Raz
  • 1,910
  • 1
  • 15
  • 28
  • No, it's not enough. The browser and server need to actually support [HTTP caching](https://en.wikipedia.org/wiki/HTTP_caching) as appropriate for the resource. It's just that `cache:false` *forces a reload*. `cache:true` does not force caching. – Bergi Apr 29 '18 at 21:45
  • No point setting contentType headers for a GET. There is no request content – charlietfl Apr 29 '18 at 21:59

1 Answers1

2

As far as I know, cache option adds a random search query to your request, this forces the browser to re-fetch the url. Normally the browser vendor will decide whether to cache or not (usually they will, so turning on the cache option will result in the desired behavior) based on the cache-control headers sent from the server.

If you want to be on the safe side, you have already stringified your data, so you can try storing it in localStorage, provided that it is smaller than 5mb and re-use it from there. For larger payloads, you will need to use indexedDb.

ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22
  • 1. What do you mean by cache option adds a random search query, so does it saves the data eventually? 2. Should I change something with that headers from the example Ive showed? 3. You mean, cache the data manually instead of using the "magical keyword": cache:true? – Raz Apr 29 '18 at 21:21
  • No no, for instance I can make a request such as `../someFile.txt`. If you request the file again, based on the cache-control header the browser will not re-request it. But if you request `../someFile.txt?123124123`, you will force the browser to re-request it. This technique is called "cache busting". So this is what the cache option does. – ibrahim tanyalcin Apr 29 '18 at 21:26
  • Yes, I request the same url, with the cache:true, I do not change the params. – Raz Apr 29 '18 at 21:55
  • Ok so then, it means you leave it to the browser to decide how long it should keep it in cache. If the default cache-control or expires header is not there, the vendor will decide how long to keep it. If the cache control header is set, then as long as you are using cache:true, you are ok till the header's expire time – ibrahim tanyalcin Apr 29 '18 at 22:00
  • Do you have a recommended cache header example? And maybe should I create own proxy checking if that specific URL was loaded in the MongoDB documents in additional to the cache? For example: check if the [some_url] is in the "cached" collection. If yes, fetch the data from my own server and result this to the autocomplete. – Raz Apr 29 '18 at 22:04
  • 1
    If you are using MongoDB probably you are using node.js, I usually use Apache + php on the backend, if the file ends with a js (+ some query) then I set the header `Header set Cache-Control "max-age=604800, public"`. This is 1 week cache (just about enough for google PSI to not complain.) In your case this [link](https://stackoverflow.com/questions/25462717/cache-control-for-dynamic-data-express-js?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) maybe can get you on track. – ibrahim tanyalcin Apr 29 '18 at 22:27