perf: lookup cache first

This commit is contained in:
Namekuji 2023-08-01 14:09:42 -04:00
parent ec05aed492
commit 4bd610be31
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
3 changed files with 26 additions and 25 deletions

View file

@ -383,25 +383,16 @@ export const UserRepository = db.getRepository(User).extend({
options, options,
); );
let user: User; const userId = typeof src === "object" ? src.id : src;
const user = await userDenormalizedCache.fetch(userId, () =>
if (typeof src === "object") {
user = src;
if (src.avatar === undefined && src.avatarId)
src.avatar = (await DriveFiles.findOneBy({ id: src.avatarId })) ?? null;
if (src.banner === undefined && src.bannerId)
src.banner = (await DriveFiles.findOneBy({ id: src.bannerId })) ?? null;
} else {
user = await userDenormalizedCache.fetch(src, () =>
this.findOneOrFail({ this.findOneOrFail({
where: { id: src }, where: { id: userId },
relations: { relations: {
avatar: true, avatar: true,
banner: true, banner: true,
}, },
}), }),
); );
}
const meId = me ? me.id : null; const meId = me ? me.id : null;
const isMe = meId === user.id; const isMe = meId === user.id;
@ -463,8 +454,9 @@ export const UserRepository = db.getRepository(User).extend({
? userInstanceCache ? userInstanceCache
.fetch( .fetch(
user.host, user.host,
() => Instances.findOneBy({ host: user.host! }), () => Instances.findOneBy({ host: user.host }),
(v) => v != null, false,
(v) => !!v,
) )
.then((instance) => .then((instance) =>
instance instance

View file

@ -30,6 +30,7 @@ import { truncate } from "@/misc/truncate.js";
import { StatusError } from "@/misc/fetch.js"; import { StatusError } from "@/misc/fetch.js";
import { import {
uriPersonCache, uriPersonCache,
userByIdCache,
userDenormalizedCache, userDenormalizedCache,
} from "@/services/user-cache.js"; } from "@/services/user-cache.js";
import { publishInternalEvent } from "@/services/stream.js"; import { publishInternalEvent } from "@/services/stream.js";
@ -525,10 +526,13 @@ export async function updatePerson(
// Update user // Update user
await Users.update(user.id, updates); await Users.update(user.id, updates);
const updatedUser = await Users.findOneByOrFail({ id: user.id }); const updatedUser = {
updatedUser.avatarId = avatar?.id ?? null; ...(await userByIdCache.fetch(user.id, () =>
Users.findOneByOrFail({ id: user.id }),
)),
...updates,
};
updatedUser.avatar = avatar; updatedUser.avatar = avatar;
updatedUser.bannerId = banner?.id ?? null;
updatedUser.banner = banner; updatedUser.banner = banner;
await userDenormalizedCache.set(updatedUser.id, updatedUser); await userDenormalizedCache.set(updatedUser.id, updatedUser);

View file

@ -308,7 +308,12 @@ export default define(meta, paramDef, async (ps, _user, token) => {
if (Object.keys(updates).length > 0) { if (Object.keys(updates).length > 0) {
await Users.update(user.id, updates); await Users.update(user.id, updates);
const data = await Users.findOneByOrFail({ id: user.id }); const data = {
...(await userByIdCache.fetch(user.id, () =>
Users.findOneByOrFail({ id: user.id }),
)),
...updates,
};
await userByIdCache.set(data.id, data); await userByIdCache.set(data.id, data);
if (data.avatarId) { if (data.avatarId) {
data.avatar = await DriveFiles.findOneBy({ id: data.avatarId }); data.avatar = await DriveFiles.findOneBy({ id: data.avatarId });