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,
);
let user: User;
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, () =>
const userId = typeof src === "object" ? src.id : src;
const user = await userDenormalizedCache.fetch(userId, () =>
this.findOneOrFail({
where: { id: src },
where: { id: userId },
relations: {
avatar: true,
banner: true,
},
}),
);
}
const meId = me ? me.id : null;
const isMe = meId === user.id;
@ -463,8 +454,9 @@ export const UserRepository = db.getRepository(User).extend({
? userInstanceCache
.fetch(
user.host,
() => Instances.findOneBy({ host: user.host! }),
(v) => v != null,
() => Instances.findOneBy({ host: user.host }),
false,
(v) => !!v,
)
.then((instance) =>
instance

View file

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