ioredis-koa v1.0.0
ioredis-koa
Redis storage for koa application or session middleware/cache,which supports cluster.
Usage
- koa middleware: redis middleware for Koa
- redis client:
ioredis-koais same as ioredis (A robust, performance-focused and full-featured Redis client for Node.js) - session:
ioredis-koaworks with koa-generic-session (a generic session middleware for koa) and supports cluster mode.
Quick Start
Install
npm install ioredis-koaExample
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
- all
ioredisoptions
Testing
- Start a Redis server on
localhost:6379. You can useredis-windowsif you are on Windows or just want a quick VM-based server. - Clone the repository and run
npm iin it (Windows should work fine). - If you want to see debug output, turn on the prompt's
DEBUGflag.
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.