perf: cache Topics.getTopicsTags

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 10ddfff38f
commit 8e0561f226

@ -33,6 +33,7 @@ module.exports = function (Topics) {
db.setAdd(`topic:${tid}:tags`, tags),
db.sortedSetsAdd(topicSets, timestamp, tid),
]);
cache.del(`topic:${tid}:tags`);
await Topics.updateCategoryTagsCount([cid], tags);
await Promise.all(tags.map(tag => updateTagCount(tag)));
};
@ -165,6 +166,7 @@ module.exports = function (Topics) {
const keys = tids.map(tid => `topic:${tid}:tags`);
await db.setsRemove(keys, tag);
await db.setsAdd(keys, newTagName);
cache.del(keys);
}, {});
await Topics.deleteTag(tag);
await updateTagCount(newTagName);
@ -235,6 +237,7 @@ module.exports = function (Topics) {
}
const keys = tids.map(tid => `topic:${tid}:tags`);
await db.setsRemove(keys, tag);
cache.del(keys);
});
}
@ -298,13 +301,29 @@ module.exports = function (Topics) {
};
Topics.getTopicTags = async function (tid) {
return await db.getSetMembers(`topic:${tid}:tags`);
const data = await Topics.getTopicsTags([tid]);
return data && data[0];
};
Topics.getTopicsTags = async function (tids) {
return await db.getSetsMembers(
tids.map(tid => `topic:${tid}:tags`)
const cachedData = {};
const uncachedKeys = cache.getUnCachedKeys(
tids.map(tid => `topic:${tid}:tags`),
cachedData
);
if (!uncachedKeys.length) {
return tids.map(tid => cachedData[`topic:${tid}:tags`].slice());
}
const tagData = await db.getSetsMembers(
uncachedKeys,
);
uncachedKeys.forEach((uncachedKey, index) => {
cachedData[uncachedKey] = tagData[index];
cache.set(uncachedKey, tagData[index]);
});
return tids.map(tid => cachedData[`topic:${tid}:tags`].slice());
};
Topics.getTopicTagsObjects = async function (tid) {
@ -346,6 +365,7 @@ module.exports = function (Topics) {
await updateTagCount(tags[i]);
}
await Topics.updateCategoryTagsCount(_.uniq(topicData.map(t => t.cid)), tags);
cache.del(sets);
};
Topics.removeTags = async function (tags, tids) {
@ -365,6 +385,7 @@ module.exports = function (Topics) {
await updateTagCount(tags[i]);
}
await Topics.updateCategoryTagsCount(_.uniq(topicData.map(t => t.cid)), tags);
cache.del(sets);
};
Topics.updateTopicTags = async function (tid, tags) {
@ -379,6 +400,7 @@ module.exports = function (Topics) {
Topics.getTopicField(tid, 'cid'),
]);
await db.delete(`topic:${tid}:tags`);
cache.del(`topic:${tid}:tags`);
const sets = tags.map(tag => `tag:${tag}:topics`)
.concat(tags.map(tag => `cid:${cid}:tag:${tag}:topics`));

Loading…
Cancel
Save