# memd

> Cache data and memoize, debounce, throttle and queue methods

Latest version **0.3.16** (published 2024-12-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install memd
pnpm add memd
yarn add memd
bun add memd
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.16 |
| Published | 2024-12-14 |
| First published | 2019-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 450.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alexander Kit |
| Maintainers | tenbits |
| Keywords | memoize, cache, debounce, throttle |

## Links

- npm: https://www.npmjs.com/package/memd
- Repository: https://github.com/atmajs/memd
- Homepage: https://github.com/atmajs/memd#readme
- Issues: https://github.com/atmajs/memd/issues
- npm.io page: https://npm.io/package/memd

## 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

- 0.3.16 (latest) — 2024-12-14
- 0.3.15 — 2024-11-12
- 0.3.14 — 2024-06-25
- 0.3.13 — 2023-03-10
- 0.3.12 — 2023-01-10
- 0.3.11 — 2022-04-26
- 0.3.10 — 2022-01-30
- 0.3.9 — 2022-01-15
- 0.3.8 — 2022-01-15
- 0.3.7 — 2021-10-12
- 0.3.6 — 2021-09-04
- 0.3.5 — 2021-07-19
- 0.3.3 — 2021-06-28
- 0.3.2 — 2021-06-28
- 0.3.1 — 2021-06-28
- … 31 more at https://npm.io/package/memd/versions

## README

# Memd

<p align='center'>
    <img src='assets/background.jpg'/>
</p>

----

[![Build Status](https://travis-ci.com/atmajs/memd.png?branch=master)](https://travis-ci.com/atmajs/memd)
[![NPM version](https://badge.fury.io/js/memd.svg)](http://badge.fury.io/js/memd)

* Memoize, debounce, throttle and queue methods
* Cache Handler
* Persistence: (File, LocalStorage, Custom)
* NodeJS/Browser

```ts

import { deco } from 'memd';

class Foo {
    @deco.memoize()
    someMethod () {}

    @deco.memoize()
    get someProp () {}

    @deco.debounce()
    bar () {}

    @deco.throttle()
    qux () {}

    @deco.queue()
    async dex () {}
}
```

### `Cache`

```ts
import { Cache } from 'memd'

interface ICacheOpts {
    maxAge?: number
    monitors?: ICacheChangeEventMonitor[]
    keyResolver?: (...args) => string
}
interface ICacheChangeEventMonitor {
    on (event: 'change', fn: Function)
    off (event: 'change', fn: Function)
}

const cache = new Cache(<ICacheOpts> { maxAge: 60 });
```
```ts
.get (key: string): T
.set (key: string, val: T): T
.clear (key?: string)
.destroy ()

```

### `memoize`

```ts
interface IMemoizeOpts {
    // Per default method or getter returns are cached for all instances of a class.
    // Use `perInstance` to cache per instance: this could be useful when the method reads `this.` values.
    perInstance?: boolean

    // When a promise is memoized, and gets rejected. Clear also the cache, so that
    // the next time it wont hit the cache and is reavaluated.
    clearOnReject?: boolean
}
.memoize(opts?: ICacheOpts & IMemoizeOpts)
```

### `debounce`

When `ms` is `0` or `undefined` then `requestAnimationFrame` or `setImmediate` is used.

```ts
.debounce(ms: number = 0)
```

### `throttle`

```ts
.throttle(ms: number, shouldCallLater?: boolean)
```

### `queued`

Calls method only when the previous promise is resolved. Use `trimQueue: true` to ensure the queue consists of max 1 listener.

```ts
.queued(opts: { trimQueue?: boolean, timeout?: number, throttle?: number })
```


## Transport and Store for Cache/Memoize

### Transport

Persist all cached data to the backed store, to be able to restore on app restarts
##### (`NodeJS`) Files

```js
import memd from 'memd'
const fs = new memd.FsTransport({ path: './lorem.json' });
class Foo {

    @memd.deco.memoize({ transport: fs })
    foo (bar) {
        // do smth
    }
}
```

##### (`Browser`) localStorage

```js
import memd from 'memd'

const storage = new memd.LocalStorageTransport({ key: 'foo' });
class Foo {

    @memd.deco.memoize({ transport: storage })
    foo (bar) {
        // do smth
    }
}
```


### Store

Read and Save single values from store

```js
import memd from 'memd'

const store = {
    getAsync (key: string, ...args) {
        // get cached value if any
        return {
            value: 'bar'
        }
    },
    saveAsync (key: string, entry: { value: any, timestamp: number }) {
        // save cached value
    }
}
class Foo {

    @memd.deco.memoize({ store })
    foo (bar) {
        // do smth
    }
}
```

----
_Atma.js Project_

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