A unique Memoization Pattern which memoizes until the next time metric - could be until the next minute, hour, day, week.
Install memoize_until
with:
npm install memoize_until --save-dev
const MemoizeUntil = require('memoize_until').MemoizeUntil
MemoizeUntil.fetch('min', 'default', () => {
return 'SomeComplexOperation';
})
-
min
: Metric until when you want to memoize. Can be one of ['min', 'hour', 'day', 'week'] -
default
: All metric levels come with a 'default' key. More can be added by initializingMemoizeUntil
with extra keys -
cb
: callback function. This callback function must return a value which needs to be memoized/cached.
To add custom keys to each metric, pass a custom object to the init
function of MemoizeUntil
.
MemoizeUntil.init({
min: ['custom1', 'custom2']
})
MemoizeUntil.fetch('min', 'custom1', () => {
return 'SomeComplexOperation';
})
To add custom keys during runtime to any metric, use the extend
function of MemoizeUntil
.
let runtime_key = 'runtime_key';
MemoizeUntil.extend('min', runtime_key)
MemoizeUntil.fetch('min', runtime_key, () => {
return 'SomeComplexOperation';
})
if the value to be memoized is undefined
or null
, MemoizeUntil
wraps it underneath a pseudo null-like object called NullObject
and always returns undefined
. This ensures that even nulls are memoized.
Ritikesh
Licensed under the MIT license.