npm.io
6.1.0 • Published 3d ago

cachette

Licence
MIT
Version
6.1.0
Deps
2
Size
184 kB
Vulns
0
Weekly
1.3K

cachette

Resilient 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.

Installation

npm install --save cachette

Basic usage

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'));

License

MIT

Keywords