[optim] more caching
This commit is contained in:
parent
2ee5d89e5f
commit
9c584ce70d
|
@ -6,19 +6,22 @@ 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) => {
|
||||
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 != ''
|
||||
WHERE p.teamName IS NOT NULL
|
||||
AND p.teamName != ''
|
||||
ORDER BY RANDOM()
|
||||
`);
|
||||
|
||||
const adminsGroupped = buildDict(function*() {
|
||||
const adminsGroupped = buildDict(function* () {
|
||||
yield [req.config.locale, []];
|
||||
for (let [locale, , , published] of locales) {
|
||||
if (locale !== req.config.locale && published) {
|
||||
|
@ -39,10 +42,12 @@ router.get('/admin/list', handleErrorAsync(async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
return res.json(adminsGroupped);
|
||||
return adminsGroupped;
|
||||
}));
|
||||
}));
|
||||
|
||||
router.get('/admin/list/footer', handleErrorAsync(async (req, res) => {
|
||||
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
|
||||
|
@ -54,7 +59,8 @@ router.get('/admin/list/footer', handleErrorAsync(async (req, res) => {
|
|||
|
||||
const fromConfig = req.config.contact.authors || [];
|
||||
|
||||
return res.json(shuffle([...fromDb, ...fromConfig]));
|
||||
return [...fromDb, ...fromConfig];
|
||||
})));
|
||||
}));
|
||||
|
||||
router.get('/admin/users', handleErrorAsync(async (req, res) => {
|
||||
|
|
|
@ -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`
|
||||
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) => {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Reference in New Issue