From a42d8c5be214dfcec91496ab340932e9a026ef62 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 15 Sep 2016 01:57:08 +0300 Subject: [PATCH] simplify keys --- src/groups/membership.js | 66 ++++++++++++------------------ src/views/admin/advanced/cache.tpl | 2 +- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/groups/membership.js b/src/groups/membership.js index 88993f0c9c..17e70a12dc 100644 --- a/src/groups/membership.js +++ b/src/groups/membership.js @@ -14,11 +14,10 @@ var pubsub = require('../pubsub'); var LRU = require('lru-cache'); var cache = LRU({ - max: 200, + max: 40000, maxAge: 1000 * 60 * 60 }); - module.exports = function(Groups) { Groups.cache = cache; @@ -80,7 +79,7 @@ module.exports = function(Groups) { async.parallel(tasks, next); }, function(results, next) { - clearCache(uid); + clearCache(uid, groupName); setGroupTitleIfNotSet(groupName, uid, next); }, function(next) { @@ -234,7 +233,7 @@ module.exports = function(Groups) { ], next); }, function(results, next) { - clearCache(uid); + clearCache(uid, groupName); Groups.getGroupFields(groupName, ['hidden', 'memberCount'], next); }, function(groupData, next) { @@ -318,13 +317,13 @@ module.exports = function(Groups) { cache.reset(); }); - function clearCache(uid) { - pubsub.publish('group:cache:del', {uid: uid}); - cache.del(uid); + function clearCache(uid, groupName) { + pubsub.publish('group:cache:del', {uid: uid, groupName: groupName}); + cache.del(uid + ':' + groupName); } pubsub.on('group:cache:del', function(data) { - cache.del(data.uid); + cache.del(data.uid + ':' + data.groupName); }); Groups.isMember = function(uid, groupName, callback) { @@ -332,10 +331,9 @@ module.exports = function(Groups) { return callback(null, false); } - var cachedData = cache.get(uid); - - if (cachedData && cachedData.hasOwnProperty(groupName)) { - return process.nextTick(callback, null, cachedData[groupName]); + var cacheKey = uid + ':' + groupName; + if (cache.has(cacheKey)) { + return process.nextTick(callback, null, cache.get(cacheKey)); } db.isSortedSetMember('group:' + groupName + ':members', uid, function(err, isMember) { @@ -343,9 +341,7 @@ module.exports = function(Groups) { return callback(err); } - cachedData = cachedData || {}; - cachedData[groupName] = isMember; - cache.set(uid, cachedData); + cache.set(cacheKey, isMember); callback(null, isMember); }); }; @@ -355,18 +351,16 @@ module.exports = function(Groups) { return callback(null, uids.map(function() {return false;})); } - var cachedUids = {}; var nonCachedUids = []; uids.forEach(function(uid) { - cachedUids[uid] = cache.get(uid); - if (!cachedUids[uid] || !cachedUids[uid].hasOwnProperty(groupName)) { + if (!cache.has(uid + ':' + groupName)) { nonCachedUids.push(uid); } }); if (!nonCachedUids.length) { var result = uids.map(function(uid) { - return cachedUids[uid] && cachedUids[uid][groupName]; + return cache.get(uid + ':' + groupName); }); return process.nextTick(callback, null, result); } @@ -377,13 +371,11 @@ module.exports = function(Groups) { } nonCachedUids.forEach(function(uid, index) { - cachedUids[uid] = cachedUids[uid] || {}; - cachedUids[uid][groupName] = isMembers[index]; - cache.set(uid, cachedUids[uid]); + cache.set(uid + ':' + groupName, isMembers[index]); }); var result = uids.map(function(uid) { - return cachedUids[uid][groupName]; + return cache.get(uid + ':' + groupName); }); callback(null, result); @@ -395,22 +387,18 @@ module.exports = function(Groups) { return callback(null, groups.map(function() {return false;})); } - var cachedData = cache.get(uid); var nonCachedGroups = []; - if (cachedData) { - groups.forEach(function(groupName) { - if (!cachedData.hasOwnProperty(groupName)) { - nonCachedGroups.push(groupName); - } - }); - } else { - nonCachedGroups = groups; - } + + groups.forEach(function(groupName) { + if (!cache.has(uid + ':' + groupName)) { + nonCachedGroups.push(groupName); + } + }); // are they all cached? - if (cachedData && !nonCachedGroups.length) { + if (!nonCachedGroups.length) { var result = groups.map(function(groupName) { - return cachedData[groupName]; + return cache.get(uid + ':' + groupName); }); return process.nextTick(callback, null, result); } @@ -423,13 +411,13 @@ module.exports = function(Groups) { if (err) { return callback(err); } - cachedData = cachedData || {}; + 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) { - return cachedData[groupName]; + return cache.get(uid + ':' + groupName); }); callback(null, result); }); diff --git a/src/views/admin/advanced/cache.tpl b/src/views/admin/advanced/cache.tpl index 4c8857483d..d8e3860367 100644 --- a/src/views/admin/advanced/cache.tpl +++ b/src/views/admin/advanced/cache.tpl @@ -26,7 +26,7 @@
Group Cache
-
+
{groupCache.itemCount}