Skip to content
Myr

Session docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent 047499d · Browse files · 3 files+73 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+73 added−12 removed

  • other:+73 added−12 removed

3 files+73 added−12 removed

modified:examples/chat/app.js+5 added-1 removed
modified:lib/express/plugins/session.js+67 added-10 removed
12// Express - Session - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34// --- Session56var Session = Class({7 8 /**9 * Initialize session _sid_.10 */11 12 init: function(sid) {13 this.id = sid14 this.touch()15 },16 17 /**18 * Update last access time.19 *20 * @api private21 */22 23 touch: function() {24 this.lastAccess = Number(new Date)12 require('sys').p('touched ' + this.id)25 }26})2728// --- MemoryStore2930exports.MemoryStore = Class({31 32 /**33 * Initialize in-memory session store.34 */35 36 init: function() {37 this.store = {}38 },39 40 /**41 * Fetch session with the given _sid_ or 42 * a new Session is returned.43 *44 * @param {int} sid45 * @return {Session}46 * @api private47 */48 49 fetch: function(sid) {50 return this.store[sid] || new Session(sid)51 },52 53 /**54 * Commit _session_ data.55 *56 * @param {Session} session57 * @api private58 */59 60 commit: function(session) {61 return this.store[session.id] = session62 },63 64 /**65 * Clear all sessions.66 *67 * @api public68 */69 70 clear: function() {71 this.store = {}72 },73 74 /**75 * Destroy session using the given _sid_.76 *77 * @param {int} sid78 * @api public79 */80 81 destroy: function(sid) {34 require('sys').p('destroying ' + sid)35 require('sys').p(this.store[sid])82 delete this.store[sid]83 },84 85 /**86 * Reap sessions older than _ms_.87 *88 * @param {int} ms89 * @api private90 */91 92 reap: function(ms) {93 var threshold = Number(new Date(Number(new Date) - ms))41 require('sys').p('reaping less than ' + threshold + ' ms')94 for (var sid in this.store)95 if (this.store.hasOwnProperty(sid))96 if (this.store[sid].lastAccess < threshold)97 this.destroy(sid)
4 unmodified lines
102 extend: {103 104 /**105 * Initialize memory store and start reaper.106 *107 * Options:108 *109 * - lifetime lifetime of session in milliseconds, defaults to one day110 * - reapInterval interval in milliseconds in which to reap old sessions, defaults to one hour111 * 112 * @param {hash} options113 * @api private114 */115 116 init: function(options) {117 process.mixin(this, options)
1 unmodified line
119 this.startReaper()120 },121 122 /**63 * Start reaping123 * Start reaper.124 *65 * @param {Type} Var66 * @return {Type}67 * @api public125 * @api private126 */127 128 startReaper: function() {129 var self = this,130 oneDay = 86400000,131 oneHour = 3600000132 setInterval(function(){75 require('sys').p('reaping')76 self.store.reap(self.expires || self.maxAge || oneDay)133 self.store.reap(self.lifetime || oneDay)134 }, self.reapInterval || oneHour)135 }136 },137
25 unmodified lines
modified:lib/express.js+1 added-1 removed