multi-process-session
Version:
manage multi process session
118 lines (102 loc) • 2.68 kB
JavaScript
// Generated by CoffeeScript 2.3.1
(function() {
var Session, _, client, redis;
redis = require("redis");
_ = require('underscore');
client = null;
Session = class Session {
constructor(sid1, options, cb) {
this.sid = sid1;
if (!client) {
client = redis.createClient(options);
}
client.get(this.sid, (err, result) => {
if (err) {
return cb(err);
}
this.session = JSON.parse(result) || {};
return cb(null);
});
}
set(key, value) {
this.session[key] = value;
return this.changed = true;
}
get(key) {
return this.session[key];
}
del(key) {
delete this.session[key];
return this.changed = true;
}
clear(cb) {
this.session = {};
return client.del(this.sid, cb);
}
setGroupName(groupName) {
this.groupName = groupName;
}
clearGroupByName(name, cb) {
var kicked_out_key, skey;
skey = 'session:ids:' + name;
kicked_out_key = "session:k:ids:" + name;
return client.smembers(skey, (err, members) => {
var afterAll;
if (err) {
return cb(err);
}
if (members.length === 0) {
return cb(null);
}
afterAll = _.after(members.length, cb);
return _.each(members, (sid) => {
return client.sadd(kicked_out_key, sid, (err) => { // added to kicked_out keys
if (err) {
return cb(err);
}
return client.del(sid, (err) => {
if (err) {
return cb(err);
}
return client.srem(skey, sid, function(err) {
if (err) {
return cb(err);
}
return afterAll();
});
});
});
});
});
}
isKickedOut(sid) {}
save(expire, cb) {
if (!this.changed) {
return cb(null);
}
return client.set(this.sid, JSON.stringify(this.session), (err) => {
if (err) {
return cb(err);
}
return client.expire(this.sid, expire, (err) => {
if (err) {
return cb(err);
}
if (this.groupName) {
return client.sadd('session:ids:' + this.groupName, this.sid, (err) => {
if (err) {
return cb(err);
}
this.changed = false;
return cb(null);
});
} else {
this.changed = false;
return cb(null);
}
});
});
}
};
module.exports = Session;
}).call(this);