# ioredis-koa

> Redis storage for koa application or session middleware/cache,which supports cluster

Latest version **1.0.0** (published 2019-07-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install ioredis-koa
pnpm add ioredis-koa
yarn add ioredis-koa
bun add ioredis-koa
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-07-15 |
| First published | 2019-02-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 2 |
| Unpacked size | 205.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | zhangke |
| Maintainers | zhang_ke |
| Keywords | koa, koa2, redis cluster, session, ioredis, redis, middleware |

## Links

- npm: https://www.npmjs.com/package/ioredis-koa
- Repository: https://github.com/godky/ioredis-koa
- Issues: https://github.com/godky/ioredis-koa/issues
- npm.io page: https://npm.io/package/ioredis-koa

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [ioredis](https://npm.io/package/ioredis.md) ^4.6.2

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

- 1.0.0 (latest) — 2019-07-15
- 0.1.9 — 2019-04-24
- 0.1.8 — 2019-04-24
- 0.1.7 — 2019-04-08
- 0.1.6 — 2019-03-21
- 0.1.5 — 2019-03-01
- 0.1.4 — 2019-02-27
- 0.1.3 — 2019-02-27
- 0.1.2 — 2019-02-26
- 0.1.1 — 2019-02-26
- 0.1.0 — 2019-02-26
- 0.0.3 — 2019-02-26
- 0.0.2 — 2019-02-26
- 0.0.1 — 2019-02-26

## README

ioredis-koa
=========

<!-- [![Coveralls][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![David devDeps][david-dev-image]][david-dev-url] -->
[![build status][travis-image]][travis-url]
[![NPM version][npm-image]][npm-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
[![license][license-image]][license-url]

[travis-image]: https://img.shields.io/travis/godky/ioredis-koa.svg?style=flat-square
[travis-url]: https://travis-ci.org/godky/ioredis-koa
[npm-image]: https://img.shields.io/npm/v/ioredis-koa.svg?style=flat-square
[npm-url]: https://npmjs.org/package/ioredis-koa
[node-image]: https://img.shields.io/node/v/ioredis-koa.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/ioredis-koa.svg?style=flat-square
[download-url]: https://npmjs.org/package/ioredis-koa
[license-image]: https://img.shields.io/npm/l/ioredis-koa.svg?style=flat-square
[license-url]: https://github.com/godky/ioredis-koa/blob/master/LICENSE
<!-- [coveralls-image]: https://img.shields.io/coveralls/godky/ioredis-koa.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/godky/ioredis-koa?branch=master
[david-image]: https://img.shields.io/david/godky/ioredis-koa.svg?style=flat-square&label=deps
[david-url]: https://david-dm.org/godky/ioredis-koa
[david-dev-image]: https://img.shields.io/david/dev/godky/ioredis-koa.svg?style=flat-square&label=devDeps
[david-dev-url]: https://david-dm.org/godky/ioredis-koa#info=devDependencies
[david-opt-image]: https://img.shields.io/david/optional/godky/ioredis-koa.svg?style=flat-square&label=optDeps
[david-opt-url]: https://david-dm.org/godky/ioredis-koa#info=devDependencies -->

Redis storage for koa application or session middleware/cache,which supports cluster.

[![NPM](https://nodei.co/npm/ioredis-koa.svg?downloads=true)](https://nodei.co/npm/ioredis-koa/)

## Usage

  - koa middleware: redis middleware for Koa
  - redis client: `ioredis-koa` is same as [ioredis](https://github.com/luin/ioredis) (A robust, performance-focused and full-featured Redis client for Node.js)
  - session: `ioredis-koa` works with [koa-generic-session](https://github.com/koajs/generic-session) (a generic session middleware for koa) and supports cluster mode.

## Quick Start

### Install

```
npm install ioredis-koa
```

### Example

#### as koa middleware

```js

var Koa = require('koa');
var redisMid = require('ioredis-koa').middleware;
var app = new Koa();

app.use(redisMid({
  port: 6379,          // Redis port
  host: '127.0.0.1',   // Redis host
  family: 4,           // 4 (IPv4) or 6 (IPv6)
  password: 'auth',
  db: 0
}))
app.use(async (ctx, next) => {
  await ctx.redis.set('foo', 1); //'ok'
  console.log(await ctx.redis.get('foo'));  // 1
})
app.listen(3000)

```

#### as a redis client

- init a single node

```js
var Redis = require('ioredis-koa');
var redis = new Redis({
  port: 6379,          // Redis port
  host: '127.0.0.1',   // Redis host
  family: 4,           // 4 (IPv4) or 6 (IPv6)
  password: 'auth',
  db: 0
});

```

- init a redis cluser

```js

var Redis = require('ioredis-koa');
var redis = new Redis([{
  port: 6380,
  host: '127.0.0.1'
}, {
  port: 6381,
  host: '127.0.0.1'
}],{
  redisOptions: {
    password: 'your-cluster-password'
  }
});

```

- useage example:

```js
redis.set('foo', 'bar');
redis.get('foo', function (err, result) {
  console.log(result);
});
 
// Or using a promise if the last argument isn't a function
redis.get('foo').then(function (result) {
  console.log(result);
});
 
// Arguments to commands are flattened, so the following are the same:
redis.sadd('set', 1, 3, 5, 7);
redis.sadd('set', [1, 3, 5, 7]);
 
// All arguments are passed directly to the redis server:
redis.set('key', 100, 'EX', 10);

```
See [more examples](https://www.npmjs.com/package/ioredis) of `ioredis`.

#### as a session middleware

These are some the funcitons that koa-generic-session uses that you can use manually. You will need to inintialize differently than the example above:

```js

var session = require('koa-generic-session');
var redisStore = require('ioredis-koa')([{
  port: 6380,
  host: '127.0.0.1'
}, {
  port: 6381,
  host: '127.0.0.1'
}],{
  redisOptions: {
    password: 'your-cluster-password'
  }
});
var app = require('koa')();

app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore
}));
```

For more examples, please see the [examples folder](https://github.com/koajs/generic-session/tree/master/example) of `koa-generic-session`.

### Options

 - all [**`ioredis`**](https://www.npmjs.com/package/ioredis)options
* [API Documentation](https://github.com/luin/ioredis/blob/HEAD/API.md)


## Testing
1. Start a Redis server on `localhost:6379`. You can use [`redis-windows`](https://github.com/ServiceStack/redis-windows) if you are on Windows or just want a quick VM-based server.
2. Clone the repository and run `npm i` in it (Windows should work fine).
3. If you want to see debug output, turn on the prompt's `DEBUG` flag.

## Authors
See the [Author](https://github.com/godky)

## Licences
(The MIT License)

Copyright (c) 2019 godky and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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