0.1.6 • Published 10 years ago
fast-sessions v0.1.6
fast-sessions
fast-sessions is a Redis session store.
Installation
npm install fast-sessions
How to use
Options
- port
optional
default: 6379. - host
optional
default: '127.0.0.1'. - client
optional
An external RedisClient object. - ttl
optional
default: 1 day. Timeout on key and sessions in milliseconds. After the timeout is expired, the key or session will be automatically deleted. - password
optional
for Redis authentication - options
optional
default: {}. Additional options. See: https://github.com/mranney/node_redis#rediscreateclientport-host-options
Initialisation
/** in app.js */
var fastSessions = require('fast-sessions');
/** first init */
fastSessions.init({ client: redis.client, ttl: 86400000 }),
/** It's the same instance which is created in the first initialization */
var fastSessions2 = require('fast-sessions');
/** fastSessions === fastSessions2. */
Methods
fastSessions.new(key, data, callback);
create new session
Arguments
@data
: (Object
required ): the data which will be stored in the new session.@callback
: (Function
required ): callback returns hash of new session.
fastSessions.get(key, params, callback);
get all the fields which are listed in params.query
Arguments
@params
: (Object
required): consists of three fields: 'query', 'session' and 'safe'(optional). 'query' is a array of fields which will be returned as an object. 'session' is a key of your session.@callback
: (Function
required): callback method with the results.
fastSessions.set(key, data, callback);
set fields
Arguments
@data
: (Object
required ): set some data to the key@callback
: (Function
required ): callback method with the results.
fastSessions.kill(key, session, callback);
kill a single session
Arguments
@session
: (String
required ): session key@callback
: (Function
required ): callback method with the results.
fastSessions.killAll(key, session, callback);
terminate all sessions without current session
Arguments
@session
: (String
required): session key@callback
: (Function
required): callback method with the results
fastSessions.unset(key, query, callback);
Arguments
@query
: (Array
required): is a array of fields which will be removed@callback
: (Function
required): callback method with the results
Examples
Create new session
fastSessions.new('key', {}, function (error, sessionKey) {
// sessionKey should be something like
// 'd4b7f99d29cb9e95be1aa9c20810e57d6211ba7f45fa072ca03027b8f547471aa1e941fc3d4b8'
});
/** or if you want to save some data to the session (note: not to the key) */
fastSessions.new('key', {
ttl: 86400000/2, // optional
user_agent: {
os: 'Mac OS',
ip: '127.0.0.1'
},
field: 'some data'
//...
},
function (error, sessionKey) {
// sessionKey ...
});
Set some data to the key
fastSessions.set('key', {
a: 'a',
b: 'b',
c: { /** some data */ }
},
function (error) {});
Get data of a key
the data will be returned only if session key exists, otherwise if you want to get the data without session check, then the "safe" field should be false
note: fastSessions automatically check the ttl of each session, and if the time has expired then the session will be deleted
fastSessions.get('key', {
query: ['a', 'b'],
session: 'session key'
},
function (error, data) {
// response should be
/** {
a: 'a',
b: 'b'
} */
});
fastSessions.get('key', {
query: ['a', 'b', 'sessions'],
safe: false // if you want to get the data without session check
},
function (error, data) {
// response
});
Get all fields of a key
fastSessions.get('key', {
query: [], // or without 'query' field
session: 'session key' // or safe: false
},
function (error, data) {
// if session key exists, response should be
/** {
a: 'a',
b: 'b',
c: { some data }
sessions: {
'd4b7f99d29cb9e95be1aa...': {},
'1ba7f45fa072ca03027b8...': {
ttl: some number,
user_agent: {
os: 'Mac OS',
ip: '127.0.0.1'
},
field: 'some data'
}
}
} */
});
License
MIT