perf: lookup cache first
This commit is contained in:
parent
ec05aed492
commit
4bd610be31
3 changed files with 26 additions and 25 deletions
|
@ -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
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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 });
|
||||||
|
|
Loading…
Reference in a new issue