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

Loading…
Cancel
Save