@danieldietrich/async-memoize-file-store
v0.3.1
Published
Filesystem store for the async-memoize library
Downloads
20
Maintainers
Readme
async-memoize-file-store
Filesystem store for the async-memoize module.
Installation
npm i @danieldietrich/async-memoize-file-store
Usage
The module supports ES6 import and CommonJS require style.
A file store is used in conjunction with the npm module @danieldietrich/async-memoize.
import memoize from '@danieldietrich/async-memoize';
import fileStore from '@danieldietrich/async-memoize-file-store';
A file store is bound to a specific directory. By default it is path.join(process.cwd(), '.file-store'))
.
const storeFactory = fileStore();
Optionally, the store directory can be changed to a different location. Please note that the parent directory is required to already exist.
const storeFactory = fileStore('/tmp/.my-cache');
A file store is used in conjunction with function memoization. For each function that is memoized, we need a unique id. Valid id characters are a-z A-Z 0-9 - . _ ~ ! $ & ' ( ) + , ; = @
. Invalid characters will be replaced with dash -
. Please use only valid characters, otherwise it might lead to name collisions.
const store = storeFactory('my-module.my-function');
Once we created a file store instance, we can start to memoize function calls.
// example
function myFunction(a: number, b: string, c: boolean): string[] { return []; }
// typesafe, memoized version of myFunction
const mem = memoize(myFunction, store);
// result is written to the file store and returned
const res = mem(1, 'ok', true);
Copyright © 2020 by Daniel Dietrich. Released under the MIT license.