Skip to content

Commit

Permalink
feat: add post-queue cache
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Feb 12, 2021
1 parent 36f2021 commit 3f35fd3
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/posts/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,47 @@ const notifications = require('../notifications');
const privileges = require('../privileges');
const plugins = require('../plugins');
const utils = require('../utils');
const cache = require('../cache');
const socketHelpers = require('../socket.io/helpers');

module.exports = function (Posts) {
Posts.getQueuedPosts = async () => {
const ids = await db.getSortedSetRange('post:queue', 0, -1);
const keys = ids.map(id => `post:queue:${id}`);
const postData = await db.getObjects(keys);
postData.forEach((data) => {
if (data) {
data.data = JSON.parse(data.data);
data.data.timestampISO = utils.toISOString(data.data.timestamp);
}
});
const uids = postData.map(data => data && data.uid);
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
postData.forEach((postData, index) => {
if (postData) {
postData.user = userData[index];
postData.data.rawContent = validator.escape(String(postData.data.content));
postData.data.title = validator.escape(String(postData.data.title || ''));
}
});
Posts.getQueuedPosts = async (filter = {}, options = {}) => {
options = { metadata: true, ...options }; // defaults
let postData;
if (cache.has('post-queue')) {
postData = cache.get('post-queue');
} else {
const ids = await db.getSortedSetRange('post:queue', 0, -1);
const keys = ids.map(id => `post:queue:${id}`);
postData = await db.getObjects(keys);
postData.forEach((data) => {
if (data) {
data.data = JSON.parse(data.data);
data.data.timestampISO = utils.toISOString(data.data.timestamp);
}
});
const uids = postData.map(data => data && data.uid);
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'picture']);
postData.forEach((postData, index) => {
if (postData) {
postData.user = userData[index];
postData.data.rawContent = validator.escape(String(postData.data.content));
postData.data.title = validator.escape(String(postData.data.title || ''));
}
});
cache.set('post-queue', postData);
}

if (options.metadata) {
await Promise.all(postData.map(p => addMetaData(p)));
}

// Filter by tid if present
if (isFinite(filter.tid)) {
const tid = parseInt(filter.tid, 10);
postData = postData.filter(item => item.data.tid && parseInt(item.data.tid, 10) === tid);
}

await Promise.all(postData.map(p => addMetaData(p)));
return postData;
};

Expand Down

1 comment on commit 3f35fd3

@barisusakli
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to clear the cache when stuff is added removed to queue

Please sign in to comment.