From a15b36c5b0012e007e7545ef77d65772a48cf50e Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Sun, 12 Jul 2020 14:37:59 +1000 Subject: [PATCH] testing cache for board list #166 --- models/pages/boardlist.js | 77 +++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/models/pages/boardlist.js b/models/pages/boardlist.js index bff26764..7b9c3fd1 100644 --- a/models/pages/boardlist.js +++ b/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,