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 = ; /** first init */fastSessions /** It's the same instance which is created in the first initialization */var fastSessions2 = ;/** 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;/** or if you want to save some data to the session (note: not to the key) */fastSessions;
Set some data to the key
fastSessions;
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;
fastSessions;
Get all fields of a key
fastSessions;
License
MIT