Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to cache data from API call with nodejs

Tags:

I am using node.js to write a web service, it calls an API for some data but I am limited by the API to a number of calls per month, so I wish to cache the data I retrieve from the API so I can serve it up with the cached data, and re-fetch the data from the API at a timed interval.

Is this a good approach for this problem? And what caching framework should I use? I looked at node-redis but I don't think a key value store is appropriate for the data.

Thanks!

like image 873
gruuuvy Avatar asked Mar 25 '13 03:03

gruuuvy


1 Answers

I would disagree with you regarding Redis. Redis is a very powerful key-value store that can easily be used for what you want. It is designed to have stuff dumped in it and taken out again. In your situation, you can easy cache the API response by saving it into Redis with the query as the key (if this is a REST API you're calling, you could just use the URL or serialized data as the key) and simply cache the response as a stringified JSON object (or XML string if you happen to be getting that).

You can also set an expiry on the cached data, and it will be cleared when the time is expired.

You could then wrap your API call in a helper function which checks the cache, and returns the value if it's present. If it's not it makes the API request, adds it to the cache, then returns it.

This is probably the most straightforward solution and seems to cover your use case pretty well.

like image 76
Nick Mitchinson Avatar answered Sep 28 '22 11:09

Nick Mitchinson