Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching JavaScript API Calls

I'm querying the GitHub API from the client using JavaScript (on this page).

There are 14 API calls each time the page loads, which means I will end up hitting GitHub's API rate limit of 5000 calls per hour pretty fast.

Most caching strategies I've seen assume that you have access to a server, but in my case I'm running a purely static Middleman site.

So my question is this: how can I cache API requests from the client? Are there third-party apps that provide this service?

(Note that my use case is many different clients hitting the page (e.g. it has been linked from Hacker News), not a single client refreshing. So local caching wouldn't really help much. )

like image 908
Sacha Avatar asked Mar 03 '14 02:03

Sacha


People also ask

Can API calls be cached?

You can enable API caching in Amazon API Gateway to cache your endpoint's responses. With caching, you can reduce the number of calls made to your endpoint and also improve the latency of requests to your API.

Should API responses be cached?

It improves response times: When a client repeatedly initiates an API without caching instructions, the API's response time is the same whether or not the data changes or not. An API with caching instructions speeds up response time because the client's first request is saved in the cache for future requests.

How do you caching in JavaScript?

You can cache a resource using three methods add , addAll , set . add() and addAll() method automatically fetches a resource, and caches it, whereas in set method we will fetch a data and set the cache. });});


2 Answers

Agreed with Firebase or separate data store alternative from @David so you can create a persistent cache mechanism since you don't have access to the server where the application sits. It's basically another data store and you can update your logic in Middleman to either make a fresh call to the Github api or to pull from data saved in Firebase based on some checks you do when a person visits that Translation page. Check out the logic here

Web sequence diagram for checking Firebase and using Github API

like image 72
d48 Avatar answered Sep 19 '22 13:09

d48


You can cache a single client's page by using local storage or a cookie. This way if the user refreshes, you can have logic to see if you want to query the API again. This would be fine if your user base was small.

This type of caching is typically done on the server since you are limiting yourself to ~357 users per hour at best.

To cache on the client side, store the data in local storage and log the time of the query. Then decide on an interval (let's say 5 minutes). Then prior to any refresh or page load, look a the users local storage and see if the query was within the last 5 minutes. If it was, read from the local storage. If not, then query the API again. This only applies to each user but by querying every 5 minutes, it would allow you to say ~30 users per hour.

http://diveintohtml5.info/storage.html

like image 28
Paul Way Avatar answered Sep 19 '22 13:09

Paul Way