Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including extensible-memoize with all npm packages installed. Try it out:

var extensibleMemoize = require("extensible-memoize")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

extensible-memoize v1.1.2

Extensible memoize that allows plugin arbitrary cache implementation, supporting multiple layers of caches. By default, it uses a mem cache with no expiration.

Build Status

Usage

import memoize from "extensible-memoize";
const getItem = memoize(fetchItem);

//define your fetch function:
function fetchItem(key: K): Promise<V> {
  //fetch the item identified by key
}

Note: if key is an object, its toString() method must return a unique hash.

Caching

By default, memoize uses a mem cache with no expiration. To specify your own caching implementation, pass an array of Caches as the second parameter.

interface Cache<K, V> {
  get: (key: K) => Promise<V>
  set: (key: K, value: V) => Promise<void>
}

const getItem = memoize(fetchItem, [cache1, cache2, ...]);

Memoize will first look for the item in cache1, then in cache2, and so on. If the item is not found in the caches, fetchItem will be called. Note that your cache implementations do not have to worry about dealing with concurrency, memoize will take care of that.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free