alltherooms / cached-request

Node.js module to perform HTTP requests with caching support
MIT License
61 stars 23 forks source link

Can I check whether cache will be hit? #26

Closed levino closed 7 years ago

levino commented 7 years ago

I am using the library to query data (and cache it) from fixer.io. Fixer.io is rate limited. So I would like to throttle requests to the api but ONLY if the data is not read from cache. Is there any way on hooking up into the case when there is cache miss?

danypype commented 7 years ago

Hi @Levino, take a look at throttled-request. You can do something like this:

var request = require('request'); 
var throttledRequest = require('throttled-request')(request);
throttledRequest.configure({requests: 1, milliseconds: 1000}); //send 1 request per second
var cachedRequest = require('cached-request')(throttledRequest);
cachedRequest.setCacheDirectory(cacheDirectory);

Then use cachedRequest to perform your requests. Only requests that are not in the cache will be throttled.

levino commented 7 years ago

Of course! Thank you for the detailed reply.