1.0.0 • Published 9 years ago
koa-observable-redis-session v1.0.0
observable-redis-session
A session module that uses Object.observe()
to set values to Redis atomically and asynchronously.
Usage:
const app = require('koa')()
require('session')(app, {
uri: 'tcp://localhost:6379'
})
app.use(function* (next) {
try {
yield* next
} catch (err) {
throw err
} finally {
// always give time for `Object.observe()` to do its thing
yield setImmediate
}
})
app.use(function* (next) {
let session = yield this.session()
session.userid = '1234'
yield* next
})
API
session(app, options)
Adds this.session()
to the app.
Options:
.uri
- Redis URI.client
- Redis client, if not set by.uri
.length
- session id key byte length, default10
.prefix
- Redis key prefix, defaulthash:koa-session:
.maxAge
- max session age, default30 days
let session = yield this.session()
Grab the session asynchronously.
session.touch()
Update the session without changing the values on the server.
session.destroy()
Destroy the session.
let session = session.regenerate()
Destroy the old session and return a new one.
session.maxAge=
Set the max age for this session, defaulting to options.maxAge
.
Caveats
- You must always
yield setImmediate
upstream to allowObject.observe()
time to execute its callback.