123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- /*
- * @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;
- }
|