Coder Social home page Coder Social logo

ioredis-koa's Introduction

ioredis-koa

build status NPM version node version npm download license

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

NPM

Usage

  • koa middleware: redis middleware for Koa
  • redis client: ioredis-koa is same as ioredis (A robust, performance-focused and full-featured Redis client for Node.js)
  • session: ioredis-koa works with koa-generic-session (a generic session middleware for koa) and supports cluster mode.

Quick Start

Install

npm install ioredis-koa

Example

as koa middleware

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

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 of koa-generic-session.

Options

Testing

  1. Start a Redis server on localhost:6379. You can use 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

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.