From 9c584ce70dcc55f16258af9c8f55e54df5c7af86 Mon Sep 17 00:00:00 2001 From: Avris Date: Thu, 17 Jun 2021 23:29:47 +0200 Subject: [PATCH] [optim] more caching --- server/routes/admin.js | 78 +++++++++++++++++++++++------------------- server/routes/terms.js | 19 +++++----- src/cache.js | 5 +-- 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/server/routes/admin.js b/server/routes/admin.js index efe9157c..9b26a792 100644 --- a/server/routes/admin.js +++ b/server/routes/admin.js @@ -6,55 +6,61 @@ import {buildDict, now, shuffle, handleErrorAsync} from "../../src/helpers"; import locales from '../../src/locales'; import {calculateStats, statsFile} from '../../src/stats'; import fs from 'fs'; +import cache from "../../src/cache"; const router = Router(); router.get('/admin/list', handleErrorAsync(async (req, res) => { - const admins = await req.db.all(SQL` - SELECT u.username, p.teamName, p.locale, u.id, u.email, u.avatarSource - FROM users u - LEFT JOIN profiles p ON p.userId = u.id - WHERE p.teamName IS NOT NULL AND p.teamName != '' - ORDER BY RANDOM() - `); + return res.json(await cache('main', 'admins.js', 10, async () => { + const admins = await req.db.all(SQL` + SELECT u.username, p.teamName, p.locale, u.id, u.email, u.avatarSource + FROM users u + LEFT JOIN profiles p ON p.userId = u.id + WHERE p.teamName IS NOT NULL + AND p.teamName != '' + ORDER BY RANDOM() + `); - const adminsGroupped = buildDict(function*() { - yield [req.config.locale, []]; - for (let [locale, , , published] of locales) { - if (locale !== req.config.locale && published) { - yield [locale, []]; + const adminsGroupped = buildDict(function* () { + yield [req.config.locale, []]; + for (let [locale, , , published] of locales) { + if (locale !== req.config.locale && published) { + yield [locale, []]; + } + } + yield ['', []]; + }); + for (let admin of admins) { + admin.avatar = await avatar(req.db, admin); + delete admin.id; + delete admin.email; + + if (adminsGroupped[admin.locale] !== undefined) { + adminsGroupped[admin.locale].push(admin); + } else { + adminsGroupped[''].push(admin); } } - yield ['', []]; - }); - for (let admin of admins) { - admin.avatar = await avatar(req.db, admin); - delete admin.id; - delete admin.email; - if (adminsGroupped[admin.locale] !== undefined) { - adminsGroupped[admin.locale].push(admin); - } else { - adminsGroupped[''].push(admin); - } - } - - return res.json(adminsGroupped); + return adminsGroupped; + })); })); router.get('/admin/list/footer', handleErrorAsync(async (req, res) => { - const fromDb = await req.db.all(SQL` - SELECT u.username, p.footerName, p.footerAreas, p.locale - FROM users u - LEFT JOIN profiles p ON p.userId = u.id - WHERE p.locale = ${req.config.locale} - AND p.footerName IS NOT NULL AND p.footerName != '' - AND p.footerAreas IS NOT NULL AND p.footerAreas != '' - `); + return res.json(shuffle(await cache('main', 'footer.js', 10, async () => { + const fromDb = await req.db.all(SQL` + SELECT u.username, p.footerName, p.footerAreas, p.locale + FROM users u + LEFT JOIN profiles p ON p.userId = u.id + WHERE p.locale = ${req.config.locale} + AND p.footerName IS NOT NULL AND p.footerName != '' + AND p.footerAreas IS NOT NULL AND p.footerAreas != '' + `); - const fromConfig = req.config.contact.authors || []; + const fromConfig = req.config.contact.authors || []; - return res.json(shuffle([...fromDb, ...fromConfig])); + return [...fromDb, ...fromConfig]; + }))); })); router.get('/admin/users', handleErrorAsync(async (req, res) => { diff --git a/server/routes/terms.js b/server/routes/terms.js index 99cd42e9..c3c31ad2 100644 --- a/server/routes/terms.js +++ b/server/routes/terms.js @@ -2,6 +2,7 @@ import { Router } from 'express'; import SQL from 'sql-template-strings'; import {ulid} from "ulid"; import {isTroll, handleErrorAsync} from "../../src/helpers"; +import cache from "../../src/cache"; const approve = async (db, id) => { const { base_id } = await db.get(SQL`SELECT base_id FROM terms WHERE id=${id}`); @@ -22,14 +23,16 @@ const approve = async (db, id) => { const router = Router(); router.get('/terms', handleErrorAsync(async (req, res) => { - return res.json(await req.db.all(SQL` - SELECT i.*, u.username AS author FROM terms i - LEFT JOIN users u ON i.author_id = u.id - WHERE i.locale = ${req.config.locale} - AND i.approved >= ${req.isGranted('terms') ? 0 : 1} - AND i.deleted = 0 - ORDER BY i.term - `)); + return res.json(await cache('main', 'terms.js', 10, () => { + return req.db.all(SQL` + SELECT i.*, u.username AS author FROM terms i + LEFT JOIN users u ON i.author_id = u.id + WHERE i.locale = ${req.config.locale} + AND i.approved >= ${req.isGranted('terms') ? 0 : 1} + AND i.deleted = 0 + ORDER BY i.term + `); + })); })); router.get('/terms/search/:term', handleErrorAsync(async (req, res) => { diff --git a/src/cache.js b/src/cache.js index 64b9b0f3..20ad09a3 100644 --- a/src/cache.js +++ b/src/cache.js @@ -6,12 +6,13 @@ export default async (dir, filename, maxAgeMinutes, generator) => { const cacheFilename = `${cacheDir}/${filename}`; if (fs.existsSync(cacheFilename) && fs.statSync(cacheFilename).mtimeMs >= (new Date() - maxAgeMinutes*60*1000)) { - return fs.readFileSync(cacheFilename); + const content = fs.readFileSync(cacheFilename); + return filename.endsWith('.js') ? JSON.parse(content) : content; } const result = await generator(); - fs.writeFileSync(cacheFilename, result); + fs.writeFileSync(cacheFilename, filename.endsWith('.js') ? JSON.stringify(result) : result); return result; }