/* * @Author: HonorLee * @Version 1.0 * @LastUpdate 2018/6/19 * @License MIT */ 'use strict' var Session = { set:function(key,value,callback){ callback = Manager.buildCallback(callback); if(!key || value==null) return callback.call(this,'Session key or value is empty',null); let _this = this; let session = {}; session.id = MD5(new Date().getTime()+''+Math.floor(Math.random()*9000+1000)); session.data = {}; session.data[key] = value; session.expire = Config.SessionExpire*60; session.expireTime = new Date().getTime()+session.expire*1000; session.autoRenewExpire = Config.SessionExpire*60; Manager.set(session,function(err){ if(err) return callback.call(_this,'Session set err;',null); callback.call(_this,null,session); }); }, setExpireTime:function(sessionid,expire,autoRenewExpire,callback){ let _this = this; callback = Manager.buildCallback(callback); if(!sessionid) return callback.call(this,'Session id is empty',null); if(!expire) return callback.call(this,'Session expire must bigger than 0',null); Session.get(sessionid,function(err,data){ if(err || !data) return callback.call(this,'Session is gone',null); let session = data; session.expire = expire*60; autoRenewExpire = autoRenewExpire*1; if(autoRenewExpire>0) session.autoRenewExpire = autoRenewExpire*60; Manager.set(session,function(err){ if(err){ LOGGER.error(err); callback.call(_this,'Session set expire error',null); }else{ callback.call(_this,null,session); } }) }); }, update:function(sessionid,key,value,callback){ callback = Manager.buildCallback(callback); if(!sessionid || !key || value==null) return callback.call(this,'Session id,key or value is empty',null); let _this = this; Manager.get(sessionid,function(err,result){ let session; if(err || result == null){ session = {id:sessionid,data:{}}; }else{ session = result; } if(err || result == null || Config.Session.AutoRefresh){ session.expire = session.autoRenewExpire; session.expireTime = new Date().getTime()+session.expire*1000; } session.data[key] = value; Manager.set(session,function(err){ if(err) return callback.call(_this,'Session set error'); callback.call(_this,null,session) }); }); }, get:function(sessionid,callback){ callback = Manager.buildCallback(callback); if(!sessionid) return callback.call(this,'Session id is empty',null); let _this = this; Manager.get(sessionid,function(err,result){ callback.call(_this,null,result); }); }, getKeyValue:function(sessionid,key,callback){ callback = Manager.buildCallback(callback); if(!sessionid || !key) return callback.call(this,'Session id or key is empty',null); let _this = this; Session.get(sessionid,function(err,result){ if(err || !result) return callback.call(_this,null); callback.call(_this,null,result.data[key]); }); }, clear:function(sessionid){ if(!sessionid) return; Manager.clear(sessionid); }, instance:function(sessionID,callback){ return new Instance(sessionID,callback); } }; module.exports = Session; var Manager = { get:function(sessionid,callback){ let session; if(Config.Session.StoreType.toLowerCase()=='memcache' && Config.Database.Memcache.on && Memcache){ Memcache.get('session_'+sessionid,function(err,result){ if(err) return callback(err); session = result; callback(null,result); if(session && Config.Session.AutoRefresh){ session.expire = session.autoRenewExpire; session.expireTime = new Date().getTime()+session.expire*1000; Manager.set(session,null); } }); }else{ try{ session = JSON.parse(FILE.readFileSync(Core.Path.Session + '/' + sessionid,'UTF-8')); }catch(e){} if(session && session.expireTime){ if(new Date().getTime()>session.expireTime){ if(Config.Session.AutoRefresh){ session.expire = session.autoRenewExpire; session.expireTime = new Date().getTime()+session.expire*1000; Manager.set(session,null); }else{ session = null; Manager.clear(sessionid); } } } if(callback) callback(null,session); } }, set:function(session,callback){ if(Config.Session.StoreType.toLowerCase()=='memcache' && Config.Database.Memcache.on && Memcache){ Memcache.set('session_'+session.id,session,session.expire,function(err){ if(err){ Logger.error(err); if(callback) callback(err); return; } if(callback) callback(); }); }else{ FILE.writeFileSync(Core.Path.Session + '/' + session.id,JSON.stringify(session),'UTF-8'); if(callback) callback(); } }, clear:function(sessionid){ if(Config.Session.StoreType.toLowerCase()=='memcache' && Config.Database.Memcache.on && Memcache){ Memcache.del('session_'+sessionid); }else{ try{ FILE.rmdirSync(Core.Path.Session + '/' + sessionid); }catch(e){} } }, buildCallback:function(callback){ if(callback && typeof callback=='function') return callback; return function(){}; } } var Instance = function(sessionID,callback){ var _data; var _sessionID = sessionID; var _this = this; this.sessionID = _sessionID; this.get = function(key){ return _data[key]; } this.set = function(key,value){ _data[key] = value; Session.update(_sessionID,key,value); } this.setExpireTime = function(expire,autoRenewExpire){ Session.setExpireTime(_sessionID,expire,autoRenewExpire); } if(sessionID){ Session.get(sessionID,function(err,session){ if(err || !session){ Session.set('status',0,function(err,new_session){ _data = new_session.data; _this.sessionID = new_session.id; if(callback) callback(new_session.id,_this); }); }else{ _data = session.data; _this.sessionID = session.id; if(callback) callback(session.id,_this); } }); }else{ Session.set('status',0,function(err,new_session){ _data = new_session.data; _sessionID = new_session.id; _this.sessionID = new_session.id; if(callback) callback(new_session.id,_this); }); } return this; }