From 54a84bf130b2172f51a8283857c7608b7daabf73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 2 Nov 2017 12:56:40 -0400 Subject: [PATCH] cache fixes --- src/database/mongo/hash.js | 20 +++++++++++++------- src/groups/membership.js | 29 +++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 15 deletions(-) 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]); });