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.
This commit is contained in:
Fabien O'Carroll 2014-06-12 13:09:35 +01:00
parent c02dfff5d2
commit 4e664ef92e

View File

@ -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);
});
});