How to cache API calls? RRS feed

  • Question

  • User1302062155 posted


    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.

    Wednesday, September 4, 2019 7:58 AM

All replies

  • User753101303 posted


    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.

    Wednesday, September 4, 2019 8:24 AM
  • User1302062155 posted

    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 for me.

    Wednesday, September 4, 2019 8:27 AM
  • User753101303 posted

    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 response.

    Wednesday, September 4, 2019 8:48 AM
  • User1302062155 posted

    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?? 

    Wednesday, September 4, 2019 9:30 AM
  • User711641945 posted

    Hi Fieel,

    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.

    Reference: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.2


    Best Regards,


    Thursday, September 5, 2019 7:31 AM
  • User753101303 posted

    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.

    Thursday, September 5, 2019 8:23 AM