パッケージの詳細

@digitalbazaar/lru-memoize

digitalbazaar33.1kBSD-3-Clause4.0.0

LRU Memoize

lru, memoize

readme

LRU-Memoize (@digitalbazaar/lru-memoize)

Node.js CI NPM Version

A Memoized wrapper around the JavaScript lru-cache library.

Table of Contents

Background

lru-memoize is used to to memoize promises (as opposed to just the results of the operations), which helps in high-concurrency use cases. (And in turn, it uses lru-cache under the hood.)

Install

To install locally (for development):

git clone https://github.com/digitalbazaar/lru-memoize.git
cd lru-memoize
npm install

Usage

To import:

import { LruCache } from '@digitalbazaar/lru-memoize';
// or
const { LruCache } = require('@digitalbazaar/lru-memoize');

The memoized LruCache constructor passes any options given to it through to the lru-cache constructor, so see that repo for the full list of cache management options. Commonly used ones include:

  • max (default: 100) - maximum size of the cache.
  • maxAge (default: 5 sec/5000 ms) - maximum age of an item in ms.
  • updateAgeOnGet (default: false) - When using time-expiring entries with maxAge, setting this to true will make each entry's effective time update to the current time whenever it is retrieved from cache, thereby extending the expiration date of the entry.

This library is useful for caching (in a deterministic memoized fashion) expensive or long-running functions, such as API requests, database lookups, and so on.

For example, say you have a function fetchStatus() that retrieves a result from a web API (here, simulated with a delay() wait). To cache the result of this function:

import { LruCache } from '@digitalbazaar/lru-memoize';

// Cache expiration/TTL: 5 seconds
const myCache = new LruCache({ maxAge: 5000 });

async function fetchStatus() {
  // simulate an async task
  await delay(100);
  executedTestFn = true;
  return {success: true, timestamp: Date.now()};
}

// Load the cached result if it's present, otherwise, perform the operation
const result = await myCache.memoize({
  key: 'myApiResults',
  fn: fetchStatus
});

// You can also memoize a particular call to a function, using anonymous arrow functions:
const url = 'https://api.example';
const result = await myCache.memoize({
  key: 'myResults',
  fn: async () => fetchMyResultsFromWeb({ url })
});

The key param is used to namespace the caches, in case the same LruCache instance is being used to cache different types of operations/functions.

Contribute

PRs accepted.

If editing the Readme, please conform to the standard-readme specification.

License

  • New BSD License (3-clause) © 2020-2023 Digital Bazaar

更新履歴

@digitalbazaar/lru-memoize ChangeLog

4.0.0 - 2025-05-22

Changed

  • BREAKING: Use lru-cache@11. This replaces lru-cache@6 which has a number of breaking changes that impact any use of this library that previously accessed the underlying cache interface. The main interface of this module has only changed in that the options it accepts when creating the cache need to now conform to v11 of lru-cache instead of v6. The v6 maxAge option, if given, will be coerced to ttl to match v11.
  • BREAKING: The delete() method now returns true if the passed key was removed from the cache and false if not, matching the v11 delete() interface. Previously, undefined was returned in both cases.

3.0.2 - 2023-08-27

Fixed

  • Only remove promise entries from cache after settling if the entry has not changed.

3.0.1 - 2023-08-27

Fixed

  • Ensure same promise is returned from memoize that is cached.

3.0.0 - 2022-06-02

Changed

  • BREAKING: Convert to module (ESM).
  • BREAKING: Require Node.js >=14.
  • Update dependencies.
  • Lint module.

2.2.0 - 2022-02-27

Added

  • Allow cache options to be passed to memoize. Only cache options that are supported by the underlying LRU instance will be used.

2.1.0 - 2021-06-30

Added

  • Add dispose on settle feature.

2.0.0 - 2021-03-02

Changed

  • BREAKING: Rename LruMemoize class to LruCache.
  • BREAKING: Use named export for LruCache.

1.1.0 - 2020-11-24

  • Implement delete API.

1.0.0 - 2020-11-23

  • See git history for details.