await-cache
A cache for async-functions. It also works for non-async functions. It's even possible to enable a LRU-algorithm.
How to install
npm i await-cache
How to use
Simply surround the async function you want to cache. In this example fetch
is a function that loads a file.
const cachify = ; const cachedFetch = { try const data1 = await // will do the real fetch and cache it for an hour const data2 = await // will instantly return the cached value const data3 = await // will fetch a different result and cache it for an hour catche // fetch throw an error console }
Second argument can be the cache-time in milliseconds or an object of options. With setting the maxSize-option you enable the LRU-Cache:
const cachify = ; const cachedFunc =
The default serialisation for the arguments uses JSON.stringify
. So you will have to write a custom serialisation if you want to use functions or complex objects as arguments.