# aedes-cached-persistence

> Abstract class to write an Aedes persistence with in-process caching of subscriptions

Latest version **10.1.1** (published 2025-05-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install aedes-cached-persistence
pnpm add aedes-cached-persistence
yarn add aedes-cached-persistence
bun add aedes-cached-persistence
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 10.1.1 |
| Published | 2025-05-12 |
| First published | 2016-02-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=20 |
| Dependencies | 3 |
| Unpacked size | 29.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Matteo Collina |
| Maintainers | matteo.collina, roberts_lando, gnought, getlarge |
| Keywords | aedes, persistence, cache, in-memory |

## Links

- npm: https://www.npmjs.com/package/aedes-cached-persistence
- Repository: https://github.com/moscajs/aedes-cached-persistence
- Homepage: https://github.com/moscajs/aedes-cached-persistence#readme
- Issues: https://github.com/moscajs/aedes-cached-persistence/issues
- npm.io page: https://npm.io/package/aedes-cached-persistence

## Dependencies (3)

- [qlobber](https://npm.io/package/qlobber.md) ^8.0.1
- [fastparallel](https://npm.io/package/fastparallel.md) ^2.4.1
- [aedes-persistence](https://npm.io/package/aedes-persistence.md) ^10.1.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

- 10.1.1 (latest) — 2025-05-12
- 10.1.0 — 2025-05-05
- 10.0.1 — 2025-04-28
- 10.0.0 — 2025-02-26
- 9.0.0 — 2022-05-17
- 8.1.1 — 2021-02-03
- 8.1.0 — 2020-02-28
- 8.0.0 — 2019-09-06
- 7.0.0 — 2018-11-20
- 6.0.0 — 2017-08-15
- 5.1.0 — 2017-07-21
- 5.0.1 — 2017-07-17
- 5.0.0 — 2017-07-17
- 4.0.0 — 2017-07-06
- 3.0.0 — 2017-04-14
- … 3 more at https://npm.io/package/aedes-cached-persistence/versions

## README

# aedes-cached-persistence
![](https://github.com/moscajs/aedes-cached-persistence/workflows/ci.yml/badge.svg)
<br/>
[![Known Vulnerabilities](https://snyk.io/test/github/moscajs/aedes-cached-persistence/badge.svg)](https://snyk.io/test/github/moscajs/aedes-cached-persistence)
[![Coverage Status](https://coveralls.io/repos/moscajs/aedes-cached-persistence/badge.svg?branch=master&service=github)](https://coveralls.io/github/moscajs/aedes-cached-persistence?branch=master)
[![NPM version](https://img.shields.io/npm/v/aedes-cached-persistence.svg?style=flat)](https://www.npmjs.com/package/aedes-cached-persistence)
[![NPM downloads](https://img.shields.io/npm/dm/aedes-cached-persistence.svg?style=flat)](https://www.npmjs.com/package/aedes-cached-persistence)

Abstract class to write an [Aedes][aedes] [persistence][aedes-persistence] with in-process caching of subscriptions.
It implements the API defined by [aedes-persistence](persistence).

- [aedes-cached-persistence](#aedes-cached-persistence)
  - [Install](#install)
  - [Provided methods](#provided-methods)
  - [Implement another persistence](#implement-another-persistence)
    - [Inheritance](#inheritance)
    - [Tests](#tests)
  - [License](#license)

## Install

To install aedes-cached-persistence, simply use npm:

```sh
npm install aedes-cached-persistence --save
```

## Provided methods

* <a href="http://github.com/moscajs/aedes-persistence#constructor"><code><b>CachedPersistence()</b></code></a>
* <a href="http://github.com/moscajs/aedes-persistence#subscriptionsByTopic"><code>instance.<b>subscriptionsByTopic()</b></code></a>
* <a href="http://github.com/moscajs/aedes-persistence#cleanSubscriptions"><code>instance.<b>cleanSubscriptions()</b></code></a>
* <a href="http://github.com/moscajs/aedes-persistence#destroy"><code>instance.<b>destroy()</b></code></a>

## Implement another persistence

### Inheritance

In order to reuse aedes-cached-persistence, you need to:

```js
const CachedPersistence = require('aedes-cached-persistence')

// if you need http://npm.im/aedes-packet, it is available
// from this module as well
// const { Packet } = CachedPersistence

class MyPersistence extends CachedPersistence {
    constructor(opts) {
       super(opts)
    }
    addSubscriptions(client, subs, cb) {
        // ..persistence specific implementation..
        // call super._addedSubscriptions when you are done
        super._addedSubscriptions(client, subs.map(mapSub), cb)
    }
    removeSubscriptions(client, subs, cb) {
        // ..persistence specific implementation..
        // call super._removedSubscriptions when you are done
        super._removedSubscriptions(client, subs.map(mapSub), cb)
    }
}

function mapSub (sub) {
  return { topic: sub.topic }
}
```

### Tests

A persistence needs to pass all tests defined in
[./abstract.js](./abstract.js). You can import and use that test suite
in the following manner:

```js
const test = require('node:test')
const myperst = require('./')
const abs = require('aedes-cached-persistence/abstract')

abs({
  test: test,
  persistence: myperst
})
```

If you require some async stuff before returning, a callback is also
supported:

```js
const test = require('node:test')
const myperst = require('./')
const abs = require('aedes-persistence/abstract')
const clean = require('./clean') // invented module

abs({
  test: test,
  buildEmitter: require('mymqemitter'), // optional
  persistence: function build (cb) {
    clean(function (err) {
      cb(err, myperst())
    })
  }
})
```

## License

MIT

[aedes]: http://npmjs.com/package/aedes
[aedes-persistence]: http://npmjs.com/package/aedes-persistence

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