Skip to content

piuccio/cached-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cached-function

Incredibly simple caching wrapper around any asynchronous function

Install

This module doesn't have any dependencies but it only works with node 10+ because it uses async/await

$ npm install cached-function

Usage

const cached = require('@piuccio/cached-function');
const fn = cached('./cache.json', async (url) {
  const { body } = await got(url);
  return body.slice(0, 100);
});

await fn('https://google.com'); // The first time it'll actually make a request
await fn('https://google.com'); // The second time it'll grab the response from cache

// In both cases the function returns the same response

The cache is is the first argument to the wrapped function so

await fn('https://google.com');
await fn('https://amazon.com');
// Will both make an actual request

About

Incredibly simple caching wrapper around any asynchronous function

Resources

Stars

Watchers

Forks

Packages

No packages published