# lru-memoizer

> Memoize functions results using an lru-cache.

Latest version **3.0.0** (published 2024-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install lru-memoizer
pnpm add lru-memoizer
yarn add lru-memoizer
bun add lru-memoizer
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2024-10-14 |
| First published | 2016-02-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 95.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 31 |
| Author | José F. Romaniello |
| Maintainers | jfromaniello |
| Keywords | cache, memoize, lru |

## Links

- npm: https://www.npmjs.com/package/lru-memoizer
- Repository: https://github.com/jfromaniello/lru-memoizer
- Homepage: https://github.com/jfromaniello/lru-memoizer#readme
- Issues: https://github.com/jfromaniello/lru-memoizer/issues
- npm.io page: https://npm.io/package/lru-memoizer

## Dependencies (2)

- [lru-cache](https://npm.io/package/lru-cache.md) ^11.0.1
- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ^4.5.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2024-10-14
- 2.3.0 — 2024-05-15
- 2.2.0 — 2023-02-08
- 2.1.4 — 2020-12-28
- 2.1.3 — 2020-12-08
- 2.1.2 — 2020-04-03
- 2.1.1 — 2020-04-03
- 2.1.0 — 2020-02-11
- 2.0.1 — 2019-07-02
- 2.0.0 — 2019-07-02
- 1.13.0 — 2019-07-02
- 1.12.0 — 2018-03-01
- 1.11.2 — 2018-02-19
- 1.11.1 — 2017-05-11
- 1.11.0 — 2017-05-11
- … 13 more at https://npm.io/package/lru-memoizer/versions

## README

Memoize functions results using an lru-cache.

## Installation

```
npm i lru-memoizer --save
```

## Intro

This module uses an [lru-cache](https://github.com/isaacs/node-lru-cache) internally to cache the results of an async function.

The `load` function can have N parameters and the last one must be a callback. The callback should be an errback (first parameter is `err`).

The `hash` function purpose is generate a custom hash for storing results. It has all the arguments applied to it minus the callback, and must return a synchronous string.

The `disable` function allows you to conditionally disable the use of the cache. Useful for test environments.

The `freeze` option (defaults to **false**) allows you to deep-freeze the result of the async function.

The `clone` option (defaults to **false**) allows you to deep-clone the result every time is returned from the cache.

## Usage

```javascript
const memoizer = require("lru-memoizer");

const memoizedGet = memoizer({
  //defines how to load the resource when
  //it is not in the cache.
  load: function (options, callback) {
    request.get(options, callback);
  },

  //defines how to create a cache key from the params.
  hash: function (options) {
    return options.url + qs.stringify(options.qs);
  },

  //don't cache in test environment
  disable: isTestEnv(),

  //all other params for the LRU cache.
  max: 100,
  ttl: 1000 * 60,
});

memoizedGet(
  {
    url: "https://google.com",
    qs: { foo: 123 },
  },
  function (err, result, body) {
    //console.log(body);
  }
);
```

## Synchronous lru-memoizer

Use `memoizer.sync` to cache things that are slow to calculate, methods returning promises, or only if you don't want to use a callback and want it synchronous.

```javascript
const memoizer = require("lru-memoizer");
const memoizedGet = memoizer.sync({
  //defines how to load the resource when
  //it is not in the cache.
  load: function (params) {
    return somethingHardToCompute();
  },

  //defines how to create a cache key from the params.
  hash: function (params) {
    return params.foo;
  },

  //all other params for the LRU cache.
  max: 100,
  ttl: 1000 * 60,
});
```

## Similar modules

This module is very similar to [async-cache](https://github.com/isaacs/async-cache)<sup>(deprecated)</sup>, the main difference is the `hash` function.

## License

MIT 2016 - José F. Romaniello

---
_Source: https://npm.io/package/lru-memoizer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
