simplify keys

v1.18.x
barisusakli 9 years ago
parent 49bd03b8ec
commit a42d8c5be2

@ -14,11 +14,10 @@ var pubsub = require('../pubsub');
var LRU = require('lru-cache'); var LRU = require('lru-cache');
var cache = LRU({ var cache = LRU({
max: 200, max: 40000,
maxAge: 1000 * 60 * 60 maxAge: 1000 * 60 * 60
}); });
module.exports = function(Groups) { module.exports = function(Groups) {
Groups.cache = cache; Groups.cache = cache;
@ -80,7 +79,7 @@ module.exports = function(Groups) {
async.parallel(tasks, next); async.parallel(tasks, next);
}, },
function(results, next) { function(results, next) {
clearCache(uid); clearCache(uid, groupName);
setGroupTitleIfNotSet(groupName, uid, next); setGroupTitleIfNotSet(groupName, uid, next);
}, },
function(next) { function(next) {
@ -234,7 +233,7 @@ module.exports = function(Groups) {
], next); ], next);
}, },
function(results, next) { function(results, next) {
clearCache(uid); clearCache(uid, groupName);
Groups.getGroupFields(groupName, ['hidden', 'memberCount'], next); Groups.getGroupFields(groupName, ['hidden', 'memberCount'], next);
}, },
function(groupData, next) { function(groupData, next) {
@ -318,13 +317,13 @@ module.exports = function(Groups) {
cache.reset(); cache.reset();
}); });
function clearCache(uid) { function clearCache(uid, groupName) {
pubsub.publish('group:cache:del', {uid: uid}); pubsub.publish('group:cache:del', {uid: uid, groupName: groupName});
cache.del(uid); cache.del(uid + ':' + groupName);
} }
pubsub.on('group:cache:del', function(data) { pubsub.on('group:cache:del', function(data) {
cache.del(data.uid); cache.del(data.uid + ':' + data.groupName);
}); });
Groups.isMember = function(uid, groupName, callback) { Groups.isMember = function(uid, groupName, callback) {
@ -332,10 +331,9 @@ module.exports = function(Groups) {
return callback(null, false); return callback(null, false);
} }
var cachedData = cache.get(uid); var cacheKey = uid + ':' + groupName;
if (cache.has(cacheKey)) {
if (cachedData && cachedData.hasOwnProperty(groupName)) { return process.nextTick(callback, null, cache.get(cacheKey));
return process.nextTick(callback, null, cachedData[groupName]);
} }
db.isSortedSetMember('group:' + groupName + ':members', uid, function(err, isMember) { db.isSortedSetMember('group:' + groupName + ':members', uid, function(err, isMember) {
@ -343,9 +341,7 @@ module.exports = function(Groups) {
return callback(err); return callback(err);
} }
cachedData = cachedData || {}; cache.set(cacheKey, isMember);
cachedData[groupName] = isMember;
cache.set(uid, cachedData);
callback(null, isMember); callback(null, isMember);
}); });
}; };
@ -355,18 +351,16 @@ module.exports = function(Groups) {
return callback(null, uids.map(function() {return false;})); return callback(null, uids.map(function() {return false;}));
} }
var cachedUids = {};
var nonCachedUids = []; var nonCachedUids = [];
uids.forEach(function(uid) { uids.forEach(function(uid) {
cachedUids[uid] = cache.get(uid); if (!cache.has(uid + ':' + groupName)) {
if (!cachedUids[uid] || !cachedUids[uid].hasOwnProperty(groupName)) {
nonCachedUids.push(uid); nonCachedUids.push(uid);
} }
}); });
if (!nonCachedUids.length) { if (!nonCachedUids.length) {
var result = uids.map(function(uid) { var result = uids.map(function(uid) {
return cachedUids[uid] && cachedUids[uid][groupName]; return cache.get(uid + ':' + groupName);
}); });
return process.nextTick(callback, null, result); return process.nextTick(callback, null, result);
} }
@ -377,13 +371,11 @@ module.exports = function(Groups) {
} }
nonCachedUids.forEach(function(uid, index) { nonCachedUids.forEach(function(uid, index) {
cachedUids[uid] = cachedUids[uid] || {}; cache.set(uid + ':' + groupName, isMembers[index]);
cachedUids[uid][groupName] = isMembers[index];
cache.set(uid, cachedUids[uid]);
}); });
var result = uids.map(function(uid) { var result = uids.map(function(uid) {
return cachedUids[uid][groupName]; return cache.get(uid + ':' + groupName);
}); });
callback(null, result); callback(null, result);
@ -395,22 +387,18 @@ module.exports = function(Groups) {
return callback(null, groups.map(function() {return false;})); return callback(null, groups.map(function() {return false;}));
} }
var cachedData = cache.get(uid);
var nonCachedGroups = []; var nonCachedGroups = [];
if (cachedData) {
groups.forEach(function(groupName) { groups.forEach(function(groupName) {
if (!cachedData.hasOwnProperty(groupName)) { if (!cache.has(uid + ':' + groupName)) {
nonCachedGroups.push(groupName); nonCachedGroups.push(groupName);
} }
}); });
} else {
nonCachedGroups = groups;
}
// are they all cached? // are they all cached?
if (cachedData && !nonCachedGroups.length) { if (!nonCachedGroups.length) {
var result = groups.map(function(groupName) { var result = groups.map(function(groupName) {
return cachedData[groupName]; return cache.get(uid + ':' + groupName);
}); });
return process.nextTick(callback, null, result); return process.nextTick(callback, null, result);
} }
@ -423,13 +411,13 @@ module.exports = function(Groups) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
cachedData = cachedData || {};
nonCachedGroups.forEach(function(groupName, index) { nonCachedGroups.forEach(function(groupName, index) {
cachedData[groupName] = isMembers[index]; cache.set(uid + ':' + groupName, isMembers[index]);
}); });
cache.set(uid, cachedData);
var result = groups.map(function(groupName) { var result = groups.map(function(groupName) {
return cachedData[groupName]; return cache.get(uid + ':' + groupName);
}); });
callback(null, result); callback(null, result);
}); });

@ -26,7 +26,7 @@
<div class="panel-heading"><i class="fa fa-calendar-o"></i> Group Cache</div> <div class="panel-heading"><i class="fa fa-calendar-o"></i> Group Cache</div>
<div class="panel-body"> <div class="panel-body">
<label>Users in Cache</label><br/> <label>Items in Cache</label><br/>
<span>{groupCache.itemCount}</span><br/> <span>{groupCache.itemCount}</span><br/>
<label>Length / Max</label><br/> <label>Length / Max</label><br/>

Loading…
Cancel
Save