diff --git a/lib/addons/memcached/index.js b/lib/addons/memcached/index.js index 423c9025..3fb25004 100644 --- a/lib/addons/memcached/index.js +++ b/lib/addons/memcached/index.js @@ -81,18 +81,21 @@ module.exports = function(app, connection) { // We're basically yaking the same idea that connects cookieSessions use // from here: http://www.senchalabs.org/connect/cookieSession.html res.on('header', function () { - var oldSessionUser = undefsafe(req, '_sessionBeforeBlacklist.user'); - if (oldSessionUser && req._userJSONCopy) { - // FIXME - this is hacky - if (JSON.stringify(oldSessionUser) !== req._userJSONCopy) { - memcached.set(oldSessionUser.name, oldSessionUser); - } - } else if (!username) { - var user = undefsafe(req, '_sessionBeforeBlacklist.user'); - if (user) { - memcached.set(user.name, user); + var sessionUser = undefsafe(req, '_sessionBeforeBlacklist.user'); + // If there's no user on the session, there's nothing to save + if (!sessionUser) { + return; + } + // If we have a copy of the user on the "way in" and it's the + // same as the user on the session now, there's no need to save + if (req._userJSONCopy) { + // FIXME - this is hacky (use a compare/equals function?) + if (JSON.stringify(sessionUser) === req._userJSONCopy) { + return; } } + // For everything else, we save them to memcache + memcached.set(sessionUser.name, sessionUser); }); });