redis-oclock
v0.1.1
Published
Downloads
13
Keywords
Readme
Redis-Oclock
Installation
yarn add redis-oclock
Usage
1. Config
- We'll assume you're using Node Config.
- Add this on your config file
development.json
// ...Others configuration
"Redis": {
"host": "localhost",
"password": "",
"port": 6379
},
"Sessions": {
"name": "lms",
"ttl": 2592000
},
Name
(String) : The app id (namespace) for this session.Ttl
(Number) : Optional - The "Time-To-Live" for the session in seconds. Default: 7200.
2. Create /src/modules/sessions.js
/*
* Package Import
*/
import config from 'config';
import Redis from 'redis-oclock';
/*
* Local Import
*/
/*
* Init
*/
const { port, host, password } = config.get('Redis');
const { name: sessionName, ttl: sessionTtl } = config.get('Sessions');
/*
* New Connection on Redis.
*/
const sessions = new Redis({ port, host, password, sessionName, sessionTtl });
/*
* Log
*/
sessions.on('connected', () => {
console.log('Connecté à la base de données REDIS');
});
sessions.on('error', () => {
console.log('Une erreur est apparue lors de la connexion à REDIS');
});
sessions.on('disconnected', () => {
console.log('La connexion à REDIS a été perdue...');
});
/*
* Export
*/
export default sessions;
3. API
get
- param :
- token
- param :
getAll
- param :
- sessionId
- param :
create
- param :
- sessionId,
- datas,
- ip
- param :
update
- param :
- token,
- datas
- param :
remove
- param :
- token,
- param :
removeAll
- param :
- sessionId
- param :
activityCount
- param :
- delay (default = 600)
- param :
activitySessions
- param :
- delay (default = 600)
- param :
removeAllSessions
4. Example
- Create Redis Session for an User.
/*
* Local Import
*/
import sessions from 'src/modules/sessions';
/*
* Code
*/
// ...Others code
// We're building object to be saved in Session.
const data = {
id: user.get('id'),
username: user.username,
// ...
};
// We're creating a session's token for this user.
return sessions.create(data.id, data, ip);