[optim] more caching

This commit is contained in:
Avris 2021-06-17 23:29:47 +02:00
parent 2ee5d89e5f
commit 9c584ce70d
3 changed files with 56 additions and 46 deletions

View File

@ -6,55 +6,61 @@ import {buildDict, now, shuffle, handleErrorAsync} from "../../src/helpers";
import locales from '../../src/locales'; import locales from '../../src/locales';
import {calculateStats, statsFile} from '../../src/stats'; import {calculateStats, statsFile} from '../../src/stats';
import fs from 'fs'; import fs from 'fs';
import cache from "../../src/cache";
const router = Router(); const router = Router();
router.get('/admin/list', handleErrorAsync(async (req, res) => { router.get('/admin/list', handleErrorAsync(async (req, res) => {
const admins = await req.db.all(SQL` return res.json(await cache('main', 'admins.js', 10, async () => {
SELECT u.username, p.teamName, p.locale, u.id, u.email, u.avatarSource const admins = await req.db.all(SQL`
FROM users u SELECT u.username, p.teamName, p.locale, u.id, u.email, u.avatarSource
LEFT JOIN profiles p ON p.userId = u.id FROM users u
WHERE p.teamName IS NOT NULL AND p.teamName != '' LEFT JOIN profiles p ON p.userId = u.id
ORDER BY RANDOM() WHERE p.teamName IS NOT NULL
`); AND p.teamName != ''
ORDER BY RANDOM()
`);
const adminsGroupped = buildDict(function*() { const adminsGroupped = buildDict(function* () {
yield [req.config.locale, []]; yield [req.config.locale, []];
for (let [locale, , , published] of locales) { for (let [locale, , , published] of locales) {
if (locale !== req.config.locale && published) { if (locale !== req.config.locale && published) {
yield [locale, []]; 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) { return adminsGroupped;
adminsGroupped[admin.locale].push(admin); }));
} else {
adminsGroupped[''].push(admin);
}
}
return res.json(adminsGroupped);
})); }));
router.get('/admin/list/footer', handleErrorAsync(async (req, res) => { router.get('/admin/list/footer', handleErrorAsync(async (req, res) => {
const fromDb = await req.db.all(SQL` return res.json(shuffle(await cache('main', 'footer.js', 10, async () => {
SELECT u.username, p.footerName, p.footerAreas, p.locale const fromDb = await req.db.all(SQL`
FROM users u SELECT u.username, p.footerName, p.footerAreas, p.locale
LEFT JOIN profiles p ON p.userId = u.id FROM users u
WHERE p.locale = ${req.config.locale} LEFT JOIN profiles p ON p.userId = u.id
AND p.footerName IS NOT NULL AND p.footerName != '' WHERE p.locale = ${req.config.locale}
AND p.footerAreas IS NOT NULL AND p.footerAreas != '' 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) => { router.get('/admin/users', handleErrorAsync(async (req, res) => {

View File

@ -2,6 +2,7 @@ import { Router } from 'express';
import SQL from 'sql-template-strings'; import SQL from 'sql-template-strings';
import {ulid} from "ulid"; import {ulid} from "ulid";
import {isTroll, handleErrorAsync} from "../../src/helpers"; import {isTroll, handleErrorAsync} from "../../src/helpers";
import cache from "../../src/cache";
const approve = async (db, id) => { const approve = async (db, id) => {
const { base_id } = await db.get(SQL`SELECT base_id FROM terms WHERE id=${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(); const router = Router();
router.get('/terms', handleErrorAsync(async (req, res) => { router.get('/terms', handleErrorAsync(async (req, res) => {
return res.json(await req.db.all(SQL` return res.json(await cache('main', 'terms.js', 10, () => {
SELECT i.*, u.username AS author FROM terms i return req.db.all(SQL`
LEFT JOIN users u ON i.author_id = u.id SELECT i.*, u.username AS author FROM terms i
WHERE i.locale = ${req.config.locale} LEFT JOIN users u ON i.author_id = u.id
AND i.approved >= ${req.isGranted('terms') ? 0 : 1} WHERE i.locale = ${req.config.locale}
AND i.deleted = 0 AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
ORDER BY i.term AND i.deleted = 0
`)); ORDER BY i.term
`);
}));
})); }));
router.get('/terms/search/:term', handleErrorAsync(async (req, res) => { router.get('/terms/search/:term', handleErrorAsync(async (req, res) => {

View File

@ -6,12 +6,13 @@ export default async (dir, filename, maxAgeMinutes, generator) => {
const cacheFilename = `${cacheDir}/${filename}`; const cacheFilename = `${cacheDir}/${filename}`;
if (fs.existsSync(cacheFilename) && fs.statSync(cacheFilename).mtimeMs >= (new Date() - maxAgeMinutes*60*1000)) { 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(); const result = await generator();
fs.writeFileSync(cacheFilename, result); fs.writeFileSync(cacheFilename, filename.endsWith('.js') ? JSON.stringify(result) : result);
return result; return result;
} }