Skip to content
Myr

More session work

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

authored by visionmedia · committed by visionmedia · parent fa37e5a · Browse files · 5 files+34 added−28 removed
This commit

State

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

Diff stats

5 files+34 added−28 removed

  • other:+34 added−28 removed

5 files+34 added−28 removed

modified:examples/chat/public/javascripts/app.js+1 added-1 removed
modified:examples/chat/views/chat.haml.html+1 added-1 removed
modified:examples/chat/app.js+5 added-4 removed
modified:lib/express/plugins/session.js+26 added-21 removed
12// Express - Session - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34function options() {5 var inOneDay = new Date(Number(new Date) + 86400)6 return set('session') || { expires: inOneDay }7}89function reaper() {10 setInterval(function(){11 exports.Session.store.reap(set('session reap threshold') || 86400)12 }, set('session reap interval') || 3600)13}144var Session = Class({5 init: function(sid) {6 this.id = sid7 this.touch()8 },9 10 touch: function() {11 this.lastAccess = Number(new Date)12 require('sys').p('touched ' + this.id)13 }14})1516exports.MemoryStore = Class({
13 unmodified lines
30 this.store = {}31 },32 33 destroy: function(sid) {34 require('sys').p('destroying ' + sid)35 require('sys').p(this.store[sid])36 delete this.store[sid]37 },38 39 reap: function(ms) {40 var threshold = Number(new Date(Number(new Date) - ms))41 require('sys').p('reaping less than ' + threshold + ' ms')42 for (var sid in this.store)43 if (this.store.hasOwnProperty(sid))44 if (this.store[sid].lastAccess < threshold)45 this.destroy(sid)
4 unmodified lines
50 extend: {51 52 /**53 * Initialize memory store and start reaper.54 */55 56 init: function(options) {57 process.mixin(this, options)58 this.store = new (this.store || exports.MemoryStore)(options)59 this.startReaper()60 },61 62 /**63 * Start reaping64 *62 * Options:63 *64 * - store: Session data-store constructor, defaults to MemoryStore 65 *66 * @param {hash} options65 * @param {Type} Var66 * @return {Type}67 * @api public68 */69 69 init: function(opts) {70 opts = opts || {}71 this.store = new (opts.store || exports.MemoryStore)(options())72 reaper()70 startReaper: function() {71 var self = this,72 oneDay = 86400000,73 oneHour = 360000074 setInterval(function(){75 require('sys').p('reaping')76 self.store.reap(self.expires || self.maxAge || oneDay)77 }, self.reapInterval || oneHour)78 }79 },80 81 // --- Events
6 unmodified lines
88 89 request: function(event) {90 var sid91 if (!(sid = event.request.cookie('sid')))87 event.request.cookie('sid', sid = uid(), options())92 event.request.cookie('sid', sid = uid(), set('session cookie'))93 event.request.session = exports.Session.store.fetch(sid)94 event.request.session.touch()95 },96
9 unmodified lines
modified:lib/express/plugin.js+1 added-1 removed