cache fixes

This commit is contained in:
Barış Soner Uşaklı
2017-11-02 12:56:40 -04:00
parent 2123e2f74c
commit 54a84bf130
2 changed files with 34 additions and 15 deletions

View File

@@ -77,9 +77,10 @@ module.exports = function (db, module) {
}; };
module.getObjects = function (keys, callback) { module.getObjects = function (keys, callback) {
function getFromCache(next) { var cachedData = {};
setImmediate(next, null, keys.map(function (key) { function getFromCache() {
return _.clone(cache.get(key)); 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) { 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; var hits = keys.length - nonCachedKeys.length;
@@ -97,7 +102,7 @@ module.exports = function (db, module) {
cache.misses += misses; cache.misses += misses;
if (!nonCachedKeys.length) { if (!nonCachedKeys.length) {
return getFromCache(callback); return getFromCache();
} }
db.collection('objects').find({ _key: { $in: nonCachedKeys } }, { _id: 0 }).toArray(function (err, data) { 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); var map = helpers.toMap(data);
nonCachedKeys.forEach(function (key) { nonCachedKeys.forEach(function (key) {
cache.set(key, map[key] || null); cachedData[key] = map[key] || null;
cache.set(key, cachedData[key]);
}); });
getFromCache(callback); getFromCache();
}); });
}; };

View File

@@ -355,8 +355,9 @@ module.exports = function (Groups) {
} }
var cacheKey = uid + ':' + groupName; var cacheKey = uid + ':' + groupName;
if (cache.has(cacheKey)) { var isMember = cache.get(cacheKey);
return setImmediate(callback, null, cache.get(cacheKey)); if (isMember !== undefined) {
return setImmediate(callback, null, isMember);
} }
async.waterfall([ async.waterfall([
@@ -371,9 +372,10 @@ module.exports = function (Groups) {
}; };
Groups.isMembers = function (uids, groupName, callback) { Groups.isMembers = function (uids, groupName, callback) {
function getFromCache(next) { var cachedData = {};
setImmediate(next, null, uids.map(function (uid) { function getFromCache() {
return cache.get(uid + ':' + groupName); 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) { 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) { if (!nonCachedUids.length) {
@@ -395,6 +401,7 @@ module.exports = function (Groups) {
}, },
function (isMembers, next) { function (isMembers, next) {
nonCachedUids.forEach(function (uid, index) { nonCachedUids.forEach(function (uid, index) {
cachedData[uid + ':' + groupName] = isMembers[index];
cache.set(uid + ':' + groupName, isMembers[index]); cache.set(uid + ':' + groupName, isMembers[index]);
}); });
@@ -404,9 +411,10 @@ module.exports = function (Groups) {
}; };
Groups.isMemberOfGroups = function (uid, groups, callback) { Groups.isMemberOfGroups = function (uid, groups, callback) {
var cachedData = {};
function getFromCache(next) { function getFromCache(next) {
setImmediate(next, null, groups.map(function (groupName) { 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) { 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) { if (!nonCachedGroups.length) {
@@ -432,6 +444,7 @@ module.exports = function (Groups) {
}, },
function (isMembers, next) { function (isMembers, next) {
nonCachedGroups.forEach(function (groupName, index) { nonCachedGroups.forEach(function (groupName, index) {
cachedData[uid + ':' + groupName] = isMembers[index];
cache.set(uid + ':' + groupName, isMembers[index]); cache.set(uid + ':' + groupName, isMembers[index]);
}); });