core: also del user cache when domain.user changes

This commit is contained in:
undefined committed 2021-05-04 12:32:26 +08:00
1 parent 7a343449b0
commit a996b6b155
4 files changed
+24 -9

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.24.5", "version": "2.24.6",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+16 -5
View File
@@ -6,6 +6,7 @@ import { ArgMethod } from '../utils';
import { MaybeArray, NumberKeys } from '../typeutils'; import { MaybeArray, NumberKeys } from '../typeutils';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import db from '../service/db'; import db from '../service/db';
import UserModel, { deleteUserCache } from './user';
const coll = db.collection('domain'); const coll = db.collection('domain');
const collUser = db.collection('domain.user'); const collUser = db.collection('domain.user');
@@ -110,11 +111,16 @@ class DomainModel {
} }
@ArgMethod @ArgMethod
static setUserRole(domainId: string, uid: MaybeArray<number>, role: string) { static async setUserRole(domainId: string, uid: MaybeArray<number>, role: string) {
if (!(uid instanceof Array)) { if (!(uid instanceof Array)) {
return collUser.updateOne({ domainId, uid }, { $set: { role } }, { upsert: true }); const res = await collUser.findOneAndUpdate({ domainId, uid }, { $set: { role } }, { upsert: true, returnOriginal: false });
deleteUserCache(res.value);
return res;
} }
return collUser.updateMany({ domainId, uid: { $in: uid } }, { $set: { role } }, { upsert: true }); const filter = { domainId, uid: { $in: uid } };
const affected = await UserModel.getMulti(filter).project({ mail: 1, uname: 1 }).toArray();
affected.forEach((udoc) => deleteUserCache(udoc));
return await collUser.updateMany(filter, { $set: { role } }, { upsert: true });
} }
static async getRoles(domainId: string, count?: boolean): Promise<any[]>; static async getRoles(domainId: string, count?: boolean): Promise<any[]>;
@@ -147,6 +153,7 @@ class DomainModel {
} }
static async setRoles(domainId: string, roles: Dictionary<bigint>) { static async setRoles(domainId: string, roles: Dictionary<bigint>) {
deleteUserCache(domainId);
const current = await DomainModel.get(domainId); const current = await DomainModel.get(domainId);
for (const role in roles) { for (const role in roles) {
current.roles[role] = roles[role].toString(); current.roles[role] = roles[role].toString();
@@ -167,6 +174,7 @@ class DomainModel {
coll.updateOne({ _id: domainId }, { $set: current }), coll.updateOne({ _id: domainId }, { $set: current }),
collUser.updateMany({ domainId, role: { $in: roles } }, { $set: { role: 'default' } }), collUser.updateMany({ domainId, role: { $in: roles } }, { $set: { role: 'default' } }),
]); ]);
deleteUserCache(domainId);
} }
static async getDomainUser(domainId: string, udoc: DomainUserArg) { static async getDomainUser(domainId: string, udoc: DomainUserArg) {
@@ -183,6 +191,7 @@ class DomainModel {
} }
static setMultiUserInDomain(domainId: string, query: any, params: any) { static setMultiUserInDomain(domainId: string, query: any, params: any) {
deleteUserCache(domainId);
return collUser.updateMany({ domainId, ...query }, { $set: params }, { upsert: true }); return collUser.updateMany({ domainId, ...query }, { $set: params }, { upsert: true });
} }
@@ -190,8 +199,10 @@ class DomainModel {
return collUser.find({ domainId, ...query }); return collUser.find({ domainId, ...query });
} }
static setUserInDomain(domainId: string, uid: number, params: any) { static async setUserInDomain(domainId: string, uid: number, params: any) {
return collUser.updateOne({ domainId, uid }, { $set: params }, { upsert: true }); const udoc = await UserModel.getById(domainId, uid);
deleteUserCache(udoc);
return await collUser.updateOne({ domainId, uid }, { $set: params }, { upsert: true });
} }
@ArgMethod @ArgMethod
+6 -2
View File
@@ -19,8 +19,12 @@ const coll: Collection<Udoc> = db.collection('user');
const logger = new Logger('model/user'); const logger = new Logger('model/user');
const cache = new LRU<string, User>({ max: 500, maxAge: 300 * 1000 }); const cache = new LRU<string, User>({ max: 500, maxAge: 300 * 1000 });
function deleteUserCache(udoc: User | Udoc) { export function deleteUserCache(udoc: User | Udoc | string) {
if (udoc) { if (typeof udoc === 'string') {
for (const key of cache.keys().filter((k) => k.endsWith(`/${udoc}`))) {
cache.del(key);
}
} else if (udoc) {
const id = [udoc._id.toString(), udoc.uname.toLowerCase(), udoc.mail.toLowerCase()]; const id = [udoc._id.toString(), udoc.uname.toLowerCase(), udoc.mail.toLowerCase()];
for (const key of cache.keys().filter((k) => id.includes(k.split('/')[0]))) { for (const key of cache.keys().filter((k) => id.includes(k.split('/')[0]))) {
cache.del(key); cache.del(key);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/onlyoffice", "name": "@hydrooj/onlyoffice",
"version": "1.0.0", "version": "1.0.1",
"main": "package.json", "main": "package.json",
"repository": "git@github.com:hydro-dev/Hydro.git", "repository": "git@github.com:hydro-dev/Hydro.git",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",