Skip to content

Node.js based forum software built for the modern web

cache fixes

Barış Soner Uşaklı54a84bf130b2
diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js
index becfb83a2f..aef54bf124 100644
--- a/src/database/mongo/hash.js
+++ b/src/database/mongo/hash.js
@@ -77,9 +77,10 @@ module.exports = function (db, module) {
};
module.getObjects = function (keys, callback) {
- function getFromCache(next) {
- setImmediate(next, null, keys.map(function (key) {
- return _.clone(cache.get(key));
+ var cachedData = {};
+ function getFromCache() {
+ process.nextTick(callback, null, keys.map(function (key) {
+ return _.clone(cachedData[key]);
}));
}
@@ -88,7 +89,11 @@ module.exports = function (db, module) {
}
var nonCachedKeys = keys.filter(function (key) {
- return cache.get(key) === undefined;
+ var data = cache.get(key);
+ if (data !== undefined) {
+ cachedData[key] = data;
+ }
+ return data === undefined;
});
var hits = keys.length - nonCachedKeys.length;
@@ -97,7 +102,7 @@ module.exports = function (db, module) {
cache.misses += misses;
if (!nonCachedKeys.length) {
- return getFromCache(callback);
+ return getFromCache();
}
db.collection('objects').find({ _key: { $in: nonCachedKeys } }, { _id: 0 }).toArray(function (err, data) {
@@ -107,10 +112,11 @@ module.exports = function (db, module) {
var map = helpers.toMap(data);
nonCachedKeys.forEach(function (key) {
- cache.set(key, map[key] || null);
+ cachedData[key] = map[key] || null;
+ cache.set(key, cachedData[key]);
});
- getFromCache(callback);
+ getFromCache();
});
};
diff --git a/src/groups/membership.js b/src/groups/membership.js
index 42c3932805..6777edfbc9 100644
--- a/src/groups/membership.js
+++ b/src/groups/membership.js
@@ -355,8 +355,9 @@ module.exports = function (Groups) {
}
var cacheKey = uid + ':' + groupName;
- if (cache.has(cacheKey)) {
- return setImmediate(callback, null, cache.get(cacheKey));
+ var isMember = cache.get(cacheKey);
+ if (isMember !== undefined) {
+ return setImmediate(callback, null, isMember);
}
async.waterfall([
@@ -371,9 +372,10 @@ module.exports = function (Groups) {
};
Groups.isMembers = function (uids, groupName, callback) {
- function getFromCache(next) {
- setImmediate(next, null, uids.map(function (uid) {
- return cache.get(uid + ':' + groupName);
+ var cachedData = {};
+ function getFromCache() {
+ setImmediate(callback, null, uids.map(function (uid) {
+ return cachedData[uid + ':' + groupName];
}));
}
@@ -382,7 +384,11 @@ module.exports = function (Groups) {
}
var nonCachedUids = uids.filter(function (uid) {
- return !cache.get(uid + ':' + groupName);
+ var isMember = cache.get(uid + ':' + groupName);
+ if (isMember !== undefined) {
+ cachedData[uid + ':' + groupName] = isMember;
+ }
+ return isMember === undefined;
});
if (!nonCachedUids.length) {
@@ -395,6 +401,7 @@ module.exports = function (Groups) {
},
function (isMembers, next) {
nonCachedUids.forEach(function (uid, index) {
+ cachedData[uid + ':' + groupName] = isMembers[index];
cache.set(uid + ':' + groupName, isMembers[index]);
});
@@ -404,9 +411,10 @@ module.exports = function (Groups) {
};
Groups.isMemberOfGroups = function (uid, groups, callback) {
+ var cachedData = {};
function getFromCache(next) {
setImmediate(next, null, groups.map(function (groupName) {
- return cache.get(uid + ':' + groupName);
+ return cachedData[uid + ':' + groupName];
}));
}
@@ -415,7 +423,11 @@ module.exports = function (Groups) {
}
var nonCachedGroups = groups.filter(function (groupName) {
- return !cache.get(uid + ':' + groupName);
+ var isMember = cache.get(uid + ':' + groupName);
+ if (isMember !== undefined) {
+ cachedData[uid + ':' + groupName] = isMember;
+ }
+ return isMember === undefined;
});
if (!nonCachedGroups.length) {
@@ -432,6 +444,7 @@ module.exports = function (Groups) {
},
function (isMembers, next) {
nonCachedGroups.forEach(function (groupName, index) {
+ cachedData[uid + ':' + groupName] = isMembers[index];
cache.set(uid + ':' + groupName, isMembers[index]);
});