Last post Sep 05, 2019 08:23 AM by PatriceSc
Sep 04, 2019 07:58 AM|Fieel|LINK
I'm building a peculiar WebAPI at the moment: I'm using data from a database, process it and call the Google directions API and then return the data to my frontend.
Because google API calls aren't free and I'll probably make similar calls repeatedly, is there a way to cache them? I want to return the cached result in case it's already been made in the past instead of making the API call again.
Sep 04, 2019 08:24 AM|PatriceSc|LINK
The developer console should allow to see at which point you are. It seems a bit harder for directions as I expect calls to be pretty unique.
There is nothing really unique here ie as usual you could use http caching if possible or your could hide this behind your own API so that you can keep a local copy and refresh or fetch new data as needed.
Sep 04, 2019 08:27 AM|Fieel|LINK
Hello, thanks, yes, I'm asking for help with http caching the Google API responses before returning my custom WebAPI response using .NET Core 2.2. I might need an example or some visualization because the usual SO answers and .NET docs didn't do it
Sep 04, 2019 08:48 AM|PatriceSc|LINK
Ok and so you are using HttpClient? From what I see Google Directions uses a GET request and I would expect this to be cached by default. My first move woould be to check the http status code for the Google request to see if I get a new or just the existing
Sep 04, 2019 09:30 AM|Fieel|LINK
I'm using https://github.com/vivet/GoogleApi, not sure if it uses HttpClient under the hood
I think i lack some fundamental knowledge here - Http responses are cached by default??
Sep 05, 2019 07:31 AM|Rena Ni|LINK
You could use in-memory cache or distributed caching in asp.net core.Or you could store the value in database when you first call api.Next time you call the same api,you could access data from the database.
Sep 05, 2019 08:23 AM|PatriceSc|LINK
Ok seems to use HttpClient (HttpEngine.cs) and according to the Google API do,c it seems direction uses a GET request. I would expect Google to send a cache directive and http client to honor this.
A quick way could be to put that in your browser and use F12 Network. I'll try to see if you can take few minutes to test this by myself and see what happens. If not cached at this level then you'll have to add this yourself.