httpcachingbrowser-cachecache-control

How to cache an api response?


I'm using the api http://exchangeratesapi.io/ to get exchange rates.

Their site asks:

Please cache results whenever possible this will allow us to keep the service without any rate limits or api key requirements.

-source

Then I found this:

By default, the responses all of the requests to the exchangeratesapi.io API are cached. This allows for significant performance improvements and reduced bandwidth from your server.

-somebody's project on github, not sure if accurate

I've never cached something before and these two statements confuse me. When the API's site says to "please cache the results", it sounds like caching is something I can do in a fetch request, or somehow on the frontend. For example, some way to store the results in local storage or something. But I couldn't find anything about how to do this. I only found resources on how to force a response NOT to cache.

The second quote makes it sound like caching is something the API does itself on their servers, since they set the response to cache automatically.

How can I cache the results like the api site asks?


Solution

  • To clear your confusion on the conflicting statements you're referencing:

    Caching just means to store the data. Examples of where the data can be stored are in memory, in some persistence layer (like Redis), or in the browser's local storage (like you mentioned). The intent behind caching can be to serve the data faster (compared to getting it from the primary data source) for future requests/fetches, and/or to save on costs for getting the same data repeatedly, among others.

    For your case, the http://exchangeratesapi.io/ API is advising consumers to cache the results on their side (as you mentioned in your question, this can be in the browser's local storage, if you're calling the API front front-end code, or stored in memory or other caching mechanisms/structures on the server-side application code calling the API) to that they can avoid the need to introduce rate limiting.

    The project from Github you're referencing, Laravel Exchange Rates, appears to be a PHP wrapper around the original API - so it's like a middleman between the API and a developer's PHP code. The intent is to make it easier to use the API from within PHP code, and avoid having to make raw HTTP requests to the API and avoid processing the responses; the Laravel Exchange Rates handles that for the developer.

    In regards to the

    By default, the responses all of the requests to the exchangeratesapi.io API are cached

    statement you're asking about, it seems the library follows the advice of the API, and caches the results from the source API.

    So, to sum up:

    1. http://exchangeratesapi.io/ is the source API, and it advises consumers to cache results. If your code is going to be calling this API, you can cache the results in your own code.
    2. The Laravel Exchange Rates PHP library is a wrapper around that source API, and does cache the results from the source API for the user. If you're using this library, you don't need to further cache.