mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: fix user cache
This commit is contained in:
1 parent
cabd3b44f9
commit
b61debc039
2 files changed
+17
-18
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.32.18",
|
"version": "2.32.19",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -30,15 +30,14 @@ export function deleteUserCache(udoc: User | Udoc | string | undefined | null, r
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (typeof udoc === 'string') {
|
if (typeof udoc === 'string') {
|
||||||
for (const key of cache.keys().filter((k) => k.endsWith(`/${udoc}`))) {
|
// is domainId
|
||||||
cache.del(key);
|
for (const key of cache.keys().filter((i) => i.endsWith(`/${udoc}`))) cache.del(key);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
const id = [`id/${udoc._id.toString()}`, `name/${udoc.uname.toLowerCase()}`, `mail/${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);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bus.on('user/delcache', (content) => deleteUserCache(JSON.parse(content), true));
|
bus.on('user/delcache', (content) => deleteUserCache(JSON.parse(content), true));
|
||||||
|
|
||||||
@@ -153,15 +152,15 @@ class UserModel {
|
|||||||
|
|
||||||
@ArgMethod
|
@ArgMethod
|
||||||
static async getById(domainId: string, _id: number, scope: bigint | string = PERM.PERM_ALL): Promise<User | null> {
|
static async getById(domainId: string, _id: number, scope: bigint | string = PERM.PERM_ALL): Promise<User | null> {
|
||||||
if (cache.has(`${_id}/${domainId}`)) return cache.get(`${_id}/${domainId}`) || null;
|
if (cache.has(`id/${_id}/${domainId}`)) return cache.get(`id/${_id}/${domainId}`) || null;
|
||||||
const udoc = await coll.findOne({ _id });
|
const udoc = await coll.findOne({ _id });
|
||||||
if (!udoc) return null;
|
if (!udoc) return null;
|
||||||
const dudoc = await domain.getDomainUser(domainId, udoc);
|
const dudoc = await domain.getDomainUser(domainId, udoc);
|
||||||
if (typeof scope === 'string') scope = BigInt(scope);
|
if (typeof scope === 'string') scope = BigInt(scope);
|
||||||
const res = await new User(udoc, dudoc, scope).init();
|
const res = await new User(udoc, dudoc, scope).init();
|
||||||
cache.set(`${res._id}/${domainId}`, res);
|
cache.set(`id/${res._id}/${domainId}`, res);
|
||||||
cache.set(`${res.uname.toLowerCase()}/${domainId}`, res);
|
cache.set(`name/${res.uname.toLowerCase()}/${domainId}`, res);
|
||||||
cache.set(`${res.mail.toLowerCase()}/${domainId}`, res);
|
cache.set(`mail/${res.mail.toLowerCase()}/${domainId}`, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,28 +175,28 @@ class UserModel {
|
|||||||
@ArgMethod
|
@ArgMethod
|
||||||
static async getByUname(domainId: string, uname: string): Promise<User | null> {
|
static async getByUname(domainId: string, uname: string): Promise<User | null> {
|
||||||
const unameLower = uname.trim().toLowerCase();
|
const unameLower = uname.trim().toLowerCase();
|
||||||
if (cache.has(`${unameLower}/${domainId}`)) return cache.get(`${unameLower}/${domainId}`) || null;
|
if (cache.has(`name/${unameLower}/${domainId}`)) return cache.get(`name/${unameLower}/${domainId}`) || null;
|
||||||
const udoc = await coll.findOne({ unameLower });
|
const udoc = await coll.findOne({ unameLower });
|
||||||
if (!udoc) return null;
|
if (!udoc) return null;
|
||||||
const dudoc = await domain.getDomainUser(domainId, udoc);
|
const dudoc = await domain.getDomainUser(domainId, udoc);
|
||||||
const res = await new UserModel.User(udoc, dudoc).init();
|
const res = await new UserModel.User(udoc, dudoc).init();
|
||||||
cache.set(`${res._id}/${domainId}`, res);
|
cache.set(`id/${res._id}/${domainId}`, res);
|
||||||
cache.set(`${res.uname.toLowerCase()}/${domainId}`, res);
|
cache.set(`name/${res.uname.toLowerCase()}/${domainId}`, res);
|
||||||
cache.set(`${res.mail.toLowerCase()}/${domainId}`, res);
|
cache.set(`mail/${res.mail.toLowerCase()}/${domainId}`, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ArgMethod
|
@ArgMethod
|
||||||
static async getByEmail(domainId: string, mail: string): Promise<User | null> {
|
static async getByEmail(domainId: string, mail: string): Promise<User | null> {
|
||||||
const mailLower = mail.trim().toLowerCase();
|
const mailLower = mail.trim().toLowerCase();
|
||||||
if (cache.has(`${mailLower}/${domainId}`)) return cache.get(`${mailLower}/${domainId}`) || null;
|
if (cache.has(`mail/${mailLower}/${domainId}`)) return cache.get(`mail/${mailLower}/${domainId}`) || null;
|
||||||
const udoc = await coll.findOne({ mailLower });
|
const udoc = await coll.findOne({ mailLower });
|
||||||
if (!udoc) return null;
|
if (!udoc) return null;
|
||||||
const dudoc = await domain.getDomainUser(domainId, udoc);
|
const dudoc = await domain.getDomainUser(domainId, udoc);
|
||||||
const res = await new UserModel.User(udoc, dudoc).init();
|
const res = await new UserModel.User(udoc, dudoc).init();
|
||||||
cache.set(`${res._id}/${domainId}`, res);
|
cache.set(`id/${res._id}/${domainId}`, res);
|
||||||
cache.set(`${res.uname.toLowerCase()}/${domainId}`, res);
|
cache.set(`name/${res.uname.toLowerCase()}/${domainId}`, res);
|
||||||
cache.set(`${res.mail.toLowerCase()}/${domainId}`, res);
|
cache.set(`mail/${res.mail.toLowerCase()}/${domainId}`, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user