session.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * @Author: HonorLee
  3. * @Version 1.0
  4. * @LastUpdate 2018/6/19
  5. * @License MIT
  6. */
  7. 'use strict'
  8. var Session = {
  9. set:function(key,value,callback){
  10. callback = Manager.buildCallback(callback);
  11. if(!key || value==null) return callback.call(this,'Session key or value is empty',null);
  12. let _this = this;
  13. let session = {};
  14. session.id = MD5(new Date().getTime()+''+Math.floor(Math.random()*9000+1000));
  15. session.data = {};
  16. session.data[key] = value;
  17. session.expire = Config.SessionExpire*60;
  18. session.expireTime = new Date().getTime()+session.expire*1000;
  19. session.autoRenewExpire = Config.SessionExpire*60;
  20. Manager.set(session,function(err){
  21. if(err) return callback.call(_this,'Session set err;',null);
  22. callback.call(_this,null,session);
  23. });
  24. },
  25. setExpireTime:function(sessionid,expire,autoRenewExpire,callback){
  26. let _this = this;
  27. callback = Manager.buildCallback(callback);
  28. if(!sessionid) return callback.call(this,'Session id is empty',null);
  29. if(!expire) return callback.call(this,'Session expire must bigger than 0',null);
  30. Session.get(sessionid,function(err,data){
  31. if(err || !data) return callback.call(this,'Session is gone',null);
  32. let session = data;
  33. session.expire = expire*60;
  34. autoRenewExpire = autoRenewExpire*1;
  35. if(autoRenewExpire>0) session.autoRenewExpire = autoRenewExpire*60;
  36. Manager.set(session,function(err){
  37. if(err){
  38. LOGGER.error(err);
  39. callback.call(_this,'Session set expire error',null);
  40. }else{
  41. callback.call(_this,null,session);
  42. }
  43. })
  44. });
  45. },
  46. update:function(sessionid,key,value,callback){
  47. callback = Manager.buildCallback(callback);
  48. if(!sessionid || !key || value==null) return callback.call(this,'Session id,key or value is empty',null);
  49. let _this = this;
  50. Manager.get(sessionid,function(err,result){
  51. let session;
  52. if(err || result == null){
  53. session = {id:sessionid,data:{}};
  54. }else{
  55. session = result;
  56. }
  57. if(err || result == null || Config.Session.AutoRefresh){
  58. session.expire = session.autoRenewExpire;
  59. session.expireTime = new Date().getTime()+session.expire*1000;
  60. }
  61. session.data[key] = value;
  62. Manager.set(session,function(err){
  63. if(err) return callback.call(_this,'Session set error');
  64. callback.call(_this,null,session)
  65. });
  66. });
  67. },
  68. get:function(sessionid,callback){
  69. callback = Manager.buildCallback(callback);
  70. if(!sessionid) return callback.call(this,'Session id is empty',null);
  71. let _this = this;
  72. Manager.get(sessionid,function(err,result){
  73. callback.call(_this,null,result);
  74. });
  75. },
  76. getKeyValue:function(sessionid,key,callback){
  77. callback = Manager.buildCallback(callback);
  78. if(!sessionid || !key) return callback.call(this,'Session id or key is empty',null);
  79. let _this = this;
  80. Session.get(sessionid,function(err,result){
  81. if(err || !result) return callback.call(_this,null);
  82. callback.call(_this,null,result.data[key]);
  83. });
  84. },
  85. clear:function(sessionid){
  86. if(!sessionid) return;
  87. Manager.clear(sessionid);
  88. },
  89. instance:function(sessionID,callback){
  90. return new Instance(sessionID,callback);
  91. }
  92. };
  93. module.exports = Session;
  94. var Manager = {
  95. get:function(sessionid,callback){
  96. let session;
  97. if(Config.Session.StoreType.toLowerCase()=='memcache' && Config.Database.Memcache.on && Memcache){
  98. Memcache.get('session_'+sessionid,function(err,result){
  99. if(err) return callback(err);
  100. session = result;
  101. callback(null,result);
  102. if(session && Config.Session.AutoRefresh){
  103. session.expire = session.autoRenewExpire;
  104. session.expireTime = new Date().getTime()+session.expire*1000;
  105. Manager.set(session,null);
  106. }
  107. });
  108. }else{
  109. try{
  110. session = JSON.parse(FILE.readFileSync(Core.Path.Session + '/' + sessionid,'UTF-8'));
  111. }catch(e){}
  112. if(session && session.expireTime){
  113. if(new Date().getTime()>session.expireTime){
  114. if(Config.Session.AutoRefresh){
  115. session.expire = session.autoRenewExpire;
  116. session.expireTime = new Date().getTime()+session.expire*1000;
  117. Manager.set(session,null);
  118. }else{
  119. session = null;
  120. Manager.clear(sessionid);
  121. }
  122. }
  123. }
  124. if(callback) callback(null,session);
  125. }
  126. },
  127. set:function(session,callback){
  128. if(Config.Session.StoreType.toLowerCase()=='memcache' && Config.Database.Memcache.on && Memcache){
  129. Memcache.set('session_'+session.id,session,session.expire,function(err){
  130. if(err){
  131. Logger.error(err);
  132. if(callback) callback(err);
  133. return;
  134. }
  135. if(callback) callback();
  136. });
  137. }else{
  138. FILE.writeFileSync(Core.Path.Session + '/' + session.id,JSON.stringify(session),'UTF-8');
  139. if(callback) callback();
  140. }
  141. },
  142. clear:function(sessionid){
  143. if(Config.Session.StoreType.toLowerCase()=='memcache' && Config.Database.Memcache.on && Memcache){
  144. Memcache.del('session_'+sessionid);
  145. }else{
  146. try{
  147. FILE.rmdirSync(Core.Path.Session + '/' + sessionid);
  148. }catch(e){}
  149. }
  150. },
  151. buildCallback:function(callback){
  152. if(callback && typeof callback=='function') return callback;
  153. return function(){};
  154. }
  155. }
  156. var Instance = function(sessionID,callback){
  157. var _data;
  158. var _sessionID = sessionID;
  159. var _this = this;
  160. this.sessionID = _sessionID;
  161. this.get = function(key){
  162. return _data[key];
  163. }
  164. this.set = function(key,value){
  165. _data[key] = value;
  166. Session.update(_sessionID,key,value);
  167. }
  168. this.setExpireTime = function(expire,autoRenewExpire){
  169. Session.setExpireTime(_sessionID,expire,autoRenewExpire);
  170. }
  171. if(sessionID){
  172. Session.get(sessionID,function(err,session){
  173. if(err || !session){
  174. Session.set('status',0,function(err,new_session){
  175. _data = new_session.data;
  176. _this.sessionID = new_session.id;
  177. if(callback) callback(new_session.id,_this);
  178. });
  179. }else{
  180. _data = session.data;
  181. _this.sessionID = session.id;
  182. if(callback) callback(session.id,_this);
  183. }
  184. });
  185. }else{
  186. Session.set('status',0,function(err,new_session){
  187. _data = new_session.data;
  188. _sessionID = new_session.id;
  189. _this.sessionID = new_session.id;
  190. if(callback) callback(new_session.id,_this);
  191. });
  192. }
  193. return this;
  194. }