mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: cache uicontext.domain
This commit is contained in:
1 parent
43fb00e3c7
commit
e39a5fa87f
8 files changed
+71
-10
No files matched your search
@@ -436,6 +436,10 @@ export const DomainApi = {
|
||||
return ddoc;
|
||||
},
|
||||
),
|
||||
'domain.current': Query(
|
||||
Schema.object({}),
|
||||
async (handler) => ({ domain: handler.domain as DomainDoc }),
|
||||
),
|
||||
groups: Query(
|
||||
Schema.object({
|
||||
domainId: Schema.string().required(),
|
||||
|
||||
@@ -13,6 +13,18 @@ import UserModel, { deleteUserCache } from './user';
|
||||
const coll = db.collection('domain');
|
||||
const collUser = db.collection('domain.user');
|
||||
const cache = new LRUCache<string, DomainDoc>({ max: 1000, ttl: 300 * 1000 });
|
||||
const cacheVersion = Symbol('domainCacheVersion');
|
||||
|
||||
function setCache(key: string, ddoc: DomainDoc | null) {
|
||||
if (ddoc) {
|
||||
Object.defineProperty(ddoc, cacheVersion, {
|
||||
value: Date.now(),
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
cache.set(key, ddoc);
|
||||
}
|
||||
|
||||
interface DomainUserArg {
|
||||
_id: number;
|
||||
@@ -22,6 +34,7 @@ interface DomainUserArg {
|
||||
class DomainModel {
|
||||
static coll = coll;
|
||||
static collUser = collUser;
|
||||
static getVersion = (ddoc) => ddoc ? ddoc[cacheVersion] : null;
|
||||
|
||||
static JOIN_METHOD_NONE = 0;
|
||||
static JOIN_METHOD_ALL = 1;
|
||||
@@ -73,7 +86,7 @@ class DomainModel {
|
||||
const result = await coll.findOne(query);
|
||||
if (result) {
|
||||
await bus.parallel('domain/get', result);
|
||||
cache.set(key, result);
|
||||
setCache(key, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -88,7 +101,7 @@ class DomainModel {
|
||||
const result = await coll.findOne(query);
|
||||
if (result) {
|
||||
await bus.parallel('domain/get', result);
|
||||
cache.set(key, result);
|
||||
setCache(key, result);
|
||||
} else {
|
||||
cache.set(key, null);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { cloneDeep, omit } from 'lodash';
|
||||
import type { KoaContext } from '@hydrooj/framework';
|
||||
import { randomPick, Time } from '@hydrooj/utils';
|
||||
import { PERM } from '../../model/builtin';
|
||||
import domain from '../../model/domain';
|
||||
import system from '../../model/system';
|
||||
import token from '../../model/token';
|
||||
|
||||
@@ -34,7 +35,7 @@ export default async (ctx: KoaContext, next: Next) => {
|
||||
UiContext.cdn_dynamic = system.get('server.cdn_dynamic');
|
||||
}
|
||||
UiContext.domainId = domainId;
|
||||
UiContext.domain = domainInfo;
|
||||
UiContext.domainVersion = domain.getVersion(domainInfo);
|
||||
ctx.HydroContext.UiContext = UiContext;
|
||||
ctx.HydroContext.domain = domainInfo;
|
||||
ctx.HydroContext.args = args;
|
||||
|
||||
Reference in new issue
Block a user