In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again โ Wikipedia
This library is an attempt to make async function, aka Promises, first class citizen with memoization
Use cases covered:
- An expensive function call (eg. API calls, intensive CPU calculations, etc)
- Multiple nodejs instances with a centralized cache (eg. Redis)
Notice: sync function can be used too
A docker cluster with multiple NodeJs nodes compute a calculation every day for each user The calculation is incremental using the data from the last 90 days. With this approach, the computation can be distributed across all the nodes available. It avoids crunching data from the previous days again and again.
npm install async-memo-ize
or
yarn add async-memo-ize
import memoize from 'async-memo-ize'
import sleep from 'sleep-promise';
const whatsTheAnswerToLifeTheUniverseAndEverything = async () => {
await sleep(2000);
return Promise.resolve(42)
}
const memoized = memoize(whatsTheAnswerToLifeTheUniverseAndEverything)
const answer = await memoized() // wait 2 seconds
const quickAnswer = await memoized() // wait ms
A simple in memory async cache based on native js Map is provided.
import memoize, {SimpleCache} from 'async-memo-ize'
const fn = async () => Promise.resolve(42)
const memoized = memoize(fn, new SimpleCache)
const answer = await memoized() // wait ms
You can provide your own implementation given the below interface:
class SimpleCache {
async has(key) {
...
}
async get(key) {
...
}
async set(key, value) {
...
}
async del(key) {
...
}
async entries() {
...
}
async size() {
...
}
}
If you want delegate and share the cache you can use RedisCache.
The generated key
is based on the function args and his name
Given:
const doSomething = async (a, b) => Promise.resolve(a+b)
The key generated:
["doSomething",1,5]
It means multiple nodejs instances can share the value computed if the function name and the args match.
import memoize, {RedisCache} from 'async-memo-ize'
const fn = async () => Promise.resolve(42)
const memoized = memoize(fn, new RedisCache())
const answer = await memoized() // wait ms
docker run --name cache-redis -d -p 6379:6379 redis:alpine
yarn test