ui: handle multi-worker cache

This commit is contained in:
undefined committed 2021-09-02 20:35:15 +08:00
1 parent b960bc9ff9
commit bc51755059
2 files changed
+15 -2

No files matched your search

+14 -1
View File
@@ -7,6 +7,7 @@ import { tmpdir } from 'os';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import * as bus from 'hydrooj/src/service/bus'; import * as bus from 'hydrooj/src/service/bus';
import { Route, Handler } from 'hydrooj/src/service/server'; import { Route, Handler } from 'hydrooj/src/service/server';
import * as db from 'hydrooj/src/service/db';
import { PERM } from 'hydrooj/src/model/builtin'; import { PERM } from 'hydrooj/src/model/builtin';
import markdown from './backendlib/markdown'; import markdown from './backendlib/markdown';
@@ -19,7 +20,17 @@ interface ConstantArgs {
domainId: string; domainId: string;
} }
declare module 'hydrooj/src/interface' {
interface Collections {
cache: {
_id: string;
value: any;
}
}
}
const cache = {}; const cache = {};
const coll = db.collection('cache');
const basedir = join(tmpdir(), 'hydro', 'public'); const basedir = join(tmpdir(), 'hydro', 'public');
async function constant(args: ConstantArgs) { async function constant(args: ConstantArgs) {
@@ -63,6 +74,7 @@ async function constant(args: ConstantArgs) {
c.update(JSON.stringify(payload)); c.update(JSON.stringify(payload));
const version = c.digest('hex'); const version = c.digest('hex');
cache[version] = { version, payload }; cache[version] = { version, payload };
await coll.updateOne({ _id: version }, { $set: { value: { version, payload } } }, { upsert: true });
return version; return version;
} }
@@ -124,7 +136,8 @@ class UiConstantsHandler extends Handler {
noCheckPermView = true; noCheckPermView = true;
async get({ version }) { async get({ version }) {
this.response.body = cache[version]; this.response.body = cache[version]
|| (await coll.findOne({ _id: version }))?.value;
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/ui-default", "name": "@hydrooj/ui-default",
"version": "4.19.1", "version": "4.19.2",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"main": "hydro.js", "main": "hydro.js",