Skip to content

Instantly share code, notes, and snippets.

@kevboutin
Created July 11, 2024 02:43
Show Gist options
  • Save kevboutin/746c292bc78184fd5d7c90018453582f to your computer and use it in GitHub Desktop.
Save kevboutin/746c292bc78184fd5d7c90018453582f to your computer and use it in GitHub Desktop.
Cachable request to memory
/* This code utilizes “cacheable-request” to cache the response from the
* specified URL for 60 seconds. Subsequent requests within that timeframe
* will retrieve the data from the cache, reducing API calls and improving
* responsiveness.
*/
const cacheableRequest = require('cacheable-request');
const cachedRequest = cacheableRequest(
requestFn,
{ ttl: 60000 } // Cache for 60 seconds
);
cachedRequest('https://api.example.com/data')
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment