Resilient cache library supporting concurrent requests through local cache or Redis.
npm install cachetteResilient cache library supporting concurrent requests through local cache or Redis.
> This library is undocumented, and only meant for internal Unito use.
> It remains licensed as MIT; TS source code is bundled in the npm tarball.
```
npm install --save cachette
`javascript
const { WriteThroughCache } = require('cachette');
// First, initialize the redis connection.
const cache = new WriteThroughCache(process.env.REDIS_URL);
async function fetchUrl(url) {
console.log('fetching', url);
const response = await fetch(url);
console.log('fetched', url);
}
async function fetchUrlCached(url) {
const fetchFunction = fetchUrl.bind(undefined, url);
return cache.getOrFetchValue(url, 600, fetchFunction);
}
fetchUrlCached('https://unito.io').then(() => console.log('first call returned'));
// First call fetches the resource, the other calls use the cached value.
fetchUrlCached('https://unito.io').then(() => console.log('second call returned'));
fetchUrlCached('https://unito.io').then(() => console.log('third call returned'));
``
MIT