Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Redis storage for koa session middleware/cache.
koa-redis
works with koa-generic-session (a generic session middleware for koa).
var session = require('koa-generic-session');
var redisStore = require('koa-redis');
var koa = require('koa');
var app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
store: redisStore({
// Options specified here
})
}));
app.use(function *() {
switch (this.path) {
case '/get':
get.call(this);
break;
case '/remove':
remove.call(this);
break;
case '/regenerate':
yield regenerate.call(this);
break;
}
});
function get() {
var session = this.session;
session.count = session.count || 0;
session.count++;
this.body = session.count;
}
function remove() {
this.session = null;
this.body = 0;
}
function *regenerate() {
get.call(this);
yield this.regenerateSession();
get.call(this);
}
app.listen(8080);
For more examples, please see the examples folder of koa-generic-session
.
node_redis
options - Useful things include url
, host
, port
, and path
to the server. Defaults to 127.0.0.1:6379
db
(number) - will run client.select(db)
after connectionclient
(object) - supply your own client, all other options are ignored unless duplicate
is also suppliedduplicate
(boolean) - When true, it will run client.duplicate(options)
on the supplied client
and use all other options supplied. This is useful if you want to select a different DB for sessions but also want to base from the same client object.serialize
- Used to serialize the data that is saved into the store.unserialize
- Used to unserialize the data that is fetched from the store.pass
and socket
have been replaced by auth_pass
and path
, but they should be backward compatible (still work).See the node_redis
docs for more info.
ready
connect
reconnecting
error
end
warning
These are some the functions that koa-generic-session
uses that you can use manually. You will need to initialize differently than the example above:
var session = require('koa-generic-session');
var redisStore = require('koa-redis')({
// Options specified here
});
var app = require('koa')();
app.keys = ['keys', 'keykeys'];
app.use(session({
store: redisStore
}));
Initialize the Redis connection with the optionally provided options (see above). The variable session
below references this.
Generator that gets a session by ID. Returns parsed JSON is exists, null
if it does not exist, and nothing upon error.
Generator that sets a JSON session by ID with an optional time-to-live (ttl) in milliseconds. Yields node_redis
's client.set()
or client.setex()
.
Generator that destroys a session (removes it from Redis) by ID. Tields node_redis
's client.del()
.
Generator that stops a Redis session after everything in the queue has completed. Yields node_redis
's client.quit()
.
Alias to session.quit()
. It is not safe to use the real end function, as it cuts off the queue.
Boolean giving the connection status updated using client.connected
after any of the events above is fired.
Direct access to the node_redis
client object.
Direct access to the co-redis
wrapper around the node_redis
client.
Server | Transaction rate | Response time |
---|---|---|
connect without session | 6763.56 trans/sec | 0.01 secs |
koa without session | 5684.75 trans/sec | 0.01 secs |
connect with session | 2759.70 trans/sec | 0.02 secs |
koa with session | 2355.38 trans/sec | 0.02 secs |
Detailed benchmark report here
localhost:6379
. You can use redis-windows
if you are on Windows or just want a quick VM-based server.npm i
in it (Windows should work fine).DEBUG
flag.npm test
to run the tests and generate coverage. To run the tests without generating coverage, run npm run-script test-only
.See the contributing tab
(The MIT License)
Copyright (c) 2015 dead-horse 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.
FAQs
Redis storage for Koa session middleware/cache with Sentinel and Cluster support
The npm package koa-redis receives a total of 23,597 weekly downloads. As such, koa-redis popularity was classified as popular.
We found that koa-redis demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.