testing cache for board list #166

merge-requests/208/head
Thomas Lynch 4 years ago
parent ed5f8b7539
commit a15b36c5b0
  1. 77
      models/pages/boardlist.js

@ -2,6 +2,7 @@
const { enableWebring } = require(__dirname+'/../../configs/main.js')
, { Boards, Webring } = require(__dirname+'/../../db/')
, cache = require(__dirname+'/../../redis.js')
, { relativeColor, relativeString } = require(__dirname+'/../../helpers/timeutils.js')
, pageQueryConverter = require(__dirname+'/../../helpers/pagequeryconverter.js')
, limit = 20;
@ -11,41 +12,55 @@ module.exports = async (req, res, next) => {
const isGlobalStaff = res.locals.permLevel <= 1;
const { page, offset, queryString } = pageQueryConverter(req.query, limit);
const direction = req.query.direction && req.query.direction === 'asc' ? 1 : -1;
let sort;
if (req.query.sort && req.query.sort === 'activity') {
sort = {
'lastPostTimestamp': direction
}
const search = (typeof req.query.search === 'string' ? req.query.search : null);
let sort = req.query.sort && req.query.sort === 'activity' ? 'activity' : 'popularity';
const cacheQuery = new URLSearchParams({ direction, sort, search, page });
cacheQuery.sort();
const cacheQueryString = cacheQuery.toString();
const cached = await cache.get(cacheQueryString);
let localBoards, webringBoards, localPages, webringPages, maxPage;
if (cached) {
({ localBoards, webringBoards, localPages, webringPages, maxPage } = cached);
} else {
sort = {
'ips': direction,
'pph': direction,
'sequence_value': direction
};
}
if (sort === 'activity') {
sort = {
'lastPostTimestamp': direction
}
} else {
sort = {
'ips': direction,
'pph': direction,
'sequence_value': direction
};
}
let filter = {};
const search = (typeof req.query.search === 'string' ? req.query.search : null);
if (req.query.search && search) {
filter = {
'search': search
let filter = {};
if (req.query.search && search) {
filter = {
'search': search
}
}
}
let localBoards, webringBoards, localPages, webringPages;
try {
[ localBoards, localPages, webringBoards, webringPages ] = await Promise.all([
Boards.boardSort(offset, limit, sort, filter, isGlobalStaff),
Boards.count(filter, isGlobalStaff),
enableWebring ? Webring.boardSort(offset, limit, sort, filter, isGlobalStaff) : null,
enableWebring ? Webring.count(filter) : 0,
]);
localPages = Math.ceil(localPages / limit);
webringPages = Math.ceil(webringPages / limit);
} catch (err) {
return next(err);
try {
[ localBoards, localPages, webringBoards, webringPages ] = await Promise.all([
Boards.boardSort(offset, limit, sort, filter, isGlobalStaff),
Boards.count(filter, isGlobalStaff),
enableWebring ? Webring.boardSort(offset, limit, sort, filter, isGlobalStaff) : null,
enableWebring ? Webring.count(filter) : 0,
]);
localPages = Math.ceil(localPages / limit);
webringPages = Math.ceil(webringPages / limit);
maxPage = Math.max(localPages, webringPages);
} catch (err) {
return next(err);
}
if (!isGlobalStaff) {
cache.set(cacheQueryString, { localBoards, localPages, webringBoards, webringPages, maxPage }, 60);
}
}
const maxPage = Math.max(localPages, webringPages);
const now = new Date();
if (localBoards) {
@ -72,7 +87,7 @@ module.exports = async (req, res, next) => {
}
res
.set('Cache-Control', 'private, max-age=60')
.set('Cache-Control', `${isGlobalStaff ? 'private' : 'public'}, max-age=60`)
.render('boardlist', {
localBoards,
webringBoards,

Loading…
Cancel
Save