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 makes async function, aka Promises, first class citizen with memoization
Use cases covered:
- An expensive function call (eg. API calls, intensive CPU calculations, etc)
- Simultaneous multiple calls are handled by a queue so that the expensive function is invoked once.
- Multiple NodeJs instances with a centralized cache (eg. Redis)
Notice: sync function can be used too
A NodeJS cluster computes a calculation every day for each user. The calculation is incremental using the data from the last 90 days. With this approach, the calculus can be distributed across all the available nodes, and the results are shared among them via a distributed cache (e.g. Redis) So that, it isn't necessary crunching data from the previous days again and again.
npm install async-memo-ize
or
yarn add async-memo-ize
Named functions
import memoize from 'async-memo-ize'
import sleep from 'sleep-promise';
const whatsTheAnswerToLifeTheUniverseAndEverything = async () => {
await sleep(2000);
return 42
}
const memoized = memoize(whatsTheAnswerToLifeTheUniverseAndEverything)
const answer = await memoized() // wait 2 seconds
const quickAnswer = await memoized() // wait ms
Anonymous functions
import memoize from 'async-memo-ize'
import sleep from 'sleep-promise';
const whatsTheAnswerToLifeTheUniverseAndEverything = memoize(async () => {
await sleep(2000);
return 42
}, {id: 'whatsTheAnswerToLifeTheUniverseAndEverything'})
const answer = await whatsTheAnswerToLifeTheUniverseAndEverything() // wait 2 seconds
const quickAnswer = await whatsTheAnswerToLifeTheUniverseAndEverything() // wait ms
If you prefer to memoize anonymous function, you have to pass a unique id
.
The id
is used to generate the cache key
and it is required to share the same cache across multiple memoized functions.
Named functions don't need because the lib rely on fn.name
as id
An async cache based on LRUMap is provided.
import memoize, {LocalCache} from 'async-memo-ize'
const fn = async () => Promise.resolve(42)
const memoized = memoize(fn, new LocalCache)
const answer = await memoized() // wait ms
You can provide your own implementation given the below interface:
class LocalCache {
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 between NodeJs instances you can use RedisCache.
yarn install async-memo-ize-plugin-redis-cache
import memoize from 'async-memo-ize'
import RedisCache from 'async-memo-ize-plugin-redis-cache'
const fn = async () => 42
const memoized = memoize(fn, new RedisCache())
const anser = await memoized()
Notice
The key
name, serialized on Redis, is based on the named function args and his name.
Given:
const doSomething = async (a, b) => 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.
If you prefer to use an anonymous function it is required to pass an id
as option
docker run -d -p 6379:6379 redis:alpine
yarn test
lerna publish
- Calculate at runtime a safe default for SimpleCache max
- Decide if or not to implement .entries() and .size on RedisCache
- Evaluate to create an ES5 compatible version
Reminder for SimpleCache max
-max_old_space_size
echo console.log(process.argv.splice(2)) > index.js
node index.js --max_old_space_size -expose_gc