@danieldietrich/async-memoize-file-store
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

npm versionvulnerabilitiesminzipped size   buildcoverage   Platform   Sponsordonatelicense   Follow

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.

Package Sidebar

Install

npm i @danieldietrich/async-memoize-file-store

Weekly Downloads

3

Version

0.3.1

License

MIT

Unpacked Size

10.2 kB

Total Files

6

Last publish

Collaborators

  • danieldietrich