From 4e664ef92ec852ee0ec32c43fe6bd8a50cd0eb15 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 12 Jun 2014 13:09:35 +0100 Subject: [PATCH] Refactor the logic for setting to memcached. First we've renamed the oldSessionUser variable to sessionUser, as that is most accurate in what we're actually doing. The logic implemented is as follows: - If there's no user on the session - don't save! - If there *is* a user on the session, and the data is the same as what we have in memcache already - don't save! - For everything else - save it. --- lib/addons/memcached/index.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) 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); }); });