Created
July 11, 2024 02:43
-
-
Save kevboutin/746c292bc78184fd5d7c90018453582f to your computer and use it in GitHub Desktop.
Cachable request to memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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