cache fixes

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 2123e2f74c
commit 54a84bf130

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

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

Loading…
Cancel
Save