memo-moo
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

MemoMoo

MemoMoo provides an easy way to cache the function results.

MemoMoo internally maintains a unique id for every passed dependency. Irrespective to the order of dependencies, it automatically sorts and finds the cached results.

memoMoo(someFunction, [dependency1, dependency2], options);
import memoMoo from 'memo-moo';

const names = ['anurag', 'anusha'];
const moreNames = ['akira', 'tara'];

function arrayToCSV() {
    console.log('CONVERTING...');
    return names.concat(moreNames).join(', ');
}

// First time:
const result1 = memoMoo(arrayToCSV, [names, moreNames]);

// Second time: Returns cached result. 
// ORDER OF DEPENDENCIES DOESN'T MATTER
const result2 = memoMoo(arrayToCSV, [moreNames, names]);

HEADS-UP: Results are cached based on the passed dependencies. You will end up getting the same result even when you pass different functions with same dependencies. To fix this, You must send an id through options.

const namesInCSV = memoMoo(arrayToCSV, [moreNames, names], {
    id: 'id1'
});
const namesObject = memoMoo(arrayToObject, [moreNames, names], {
    id: 'id2'
});

By default results are cached for 300 seconds. It can be configured.

memoMoo(arrayToCSV, [moreNames, names], {
   expiresIn: 60 // Seconds 
});

Garbage collector cleans the expired data after 250 seconds. It can be configured

import memoMoo, { setGCTimeout } from 'memoMoo';

setGCTimeout(30); // Now GC will clean expired data after 30 seconds

memoMoo(() => {
    console.log('nothing..');
}, []);

Readme

Keywords

Package Sidebar

Install

npm i memo-moo

Weekly Downloads

2

Version

1.1.1

License

ISC

Unpacked Size

5.42 kB

Total Files

6

Last publish

Collaborators

  • anurag6713