# aedes1cached-persistence

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

Latest version **2.1.0** (published 2017-03-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install aedes1cached-persistence
pnpm add aedes1cached-persistence
yarn add aedes1cached-persistence
bun add aedes1cached-persistence
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2017-03-15 |
| First published | 2017-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Matteo Collina |
| Maintainers | matteo.collina |
| Keywords | aedes, persistence, cache, in-memory |

## Links

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

## Dependencies (4)

- [qlobber](https://npm.io/package/qlobber.md) ^0.7.0
- [aedes-packet](https://npm.io/package/aedes-packet.md) ^1.0.0
- [fastparallel](https://npm.io/package/fastparallel.md) ^2.2.1
- [aedes-persistence](https://npm.io/package/aedes-persistence.md) ^2.4.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

- 2.1.0 (latest) — 2017-03-15

## README

# aedes-cached-persistence&nbsp;&nbsp;[![Build Status](https://travis-ci.org/mcollina/aedes-cached-persistence.svg)](https://travis-ci.org/mcollina/aedes-cached-persistence)

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

* [Install](#install)
* [Provided methods](#api)
* [Implement another persistence](#implement)
* [License](#license)

<a name="install"></a>
## Install
To install aedes-cached--persistence, simply use npm:

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

## Provided methods

  * <a href="http://github.com/mcollina/aedes-persistence#constructor"><code><b>CachedPersistence()</b></code></a>
  * <a href="http://github.com/mcollina/aedes-persistence#subscriptionsByTopic"><code>instance.<b>subscriptionsByTopic()</b></code></a>
  * <a href="http://github.com/mcollina/aedes-persistence#cleanSubscriptions"><code>instance.<b>cleanSubscriptions()</b></code></a>
  * <a href="http://github.com/mcollina/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
'use strict'

var util = require('util')
var CachedPersistence = require('aedes-cached-persistence')

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

function MyPersistence (opts) {
  if (!(this instanceof MyPersistence)) {
    return new MyPersistence(opts)
  }
  // initialize your data here

  CachedPersistence.call(this, opts)
}

util.inherits(MyPersistence, CachedPersistence)

MyPersistence.prototype.addSubscriptions = function (client, subs, cb) {
  // ..persistence specific implementation..

  // call this._addedSubscriptions when you are done
  this._addedSubscriptions(client, subsObjs, cb)
}

MyPersistence.prototype.removeSubscriptions = function (client, subs, cb) {
  // ..persistence specific implementation..

  // call this._addedSubscriptions when you are done
  this._removedSubscriptions(client, subs.map(subs, client), cb)
}

function toSubObj (sub) {
  return {
    clientId: this.id,
    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
var test = require('tape').test
var myperst = require('./')
var 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
var test = require('tape').test
var myperst = require('./')
var abs = require('aedes-persistence/abstract')
var 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://npm.im/aedes
[aedes-persistence]: http://npm.im/aedes-persistence

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