3 * Connect - session - MemoryStore
4 * Copyright(c) 2010 Sencha Inc.
5 * Copyright(c) 2011 TJ Holowaychuk
10 * Module dependencies.
13 var Store = require('./store')
14 , utils = require('../../utils')
15 , Session = require('./session');
18 * Initialize a new `MemoryStore`.
23 var MemoryStore = module.exports = function MemoryStore() {
28 * Inherit from `Store.prototype`.
31 MemoryStore.prototype.__proto__ = Store.prototype;
34 * Attempt to fetch session by the given `sid`.
37 * @param {Function} fn
41 MemoryStore.prototype.get = function(sid, fn){
43 process.nextTick(function(){
45 , sess = self.sessions[sid];
47 sess = JSON.parse(sess);
48 expires = 'string' == typeof sess.cookie.expires
49 ? new Date(sess.cookie.expires)
50 : sess.cookie.expires;
51 if (!expires || new Date < expires) {
54 self.destroy(sid, fn);
63 * Commit the given `sess` object associated with the given `sid`.
66 * @param {Session} sess
67 * @param {Function} fn
71 MemoryStore.prototype.set = function(sid, sess, fn){
73 process.nextTick(function(){
74 self.sessions[sid] = JSON.stringify(sess);
80 * Destroy the session associated with the given `sid`.
86 MemoryStore.prototype.destroy = function(sid, fn){
88 process.nextTick(function(){
89 delete self.sessions[sid];
95 * Invoke the given callback `fn` with all active sessions.
97 * @param {Function} fn
101 MemoryStore.prototype.all = function(fn){
103 , keys = Object.keys(this.sessions);
104 for (var i = 0, len = keys.length; i < len; ++i) {
105 arr.push(this.sessions[keys[i]]);
111 * Clear all sessions.
113 * @param {Function} fn
117 MemoryStore.prototype.clear = function(fn){
123 * Fetch number of sessions.
125 * @param {Function} fn
129 MemoryStore.prototype.length = function(fn){
130 fn(null, Object.keys(this.sessions).length);