perf(backend): improve cache of instance of user

This commit is contained in:
syuilo 2023-04-07 18:12:24 +09:00
parent 1e3fb5531b
commit 3bbeec70ec

View file

@ -9,7 +9,7 @@ import type { Packed } from '@/misc/json-schema.js';
import type { Promiseable } from '@/misc/prelude/await-all.js'; import type { Promiseable } from '@/misc/prelude/await-all.js';
import { awaitAll } from '@/misc/prelude/await-all.js'; import { awaitAll } from '@/misc/prelude/await-all.js';
import { USER_ACTIVE_THRESHOLD, USER_ONLINE_THRESHOLD } from '@/const.js'; import { USER_ACTIVE_THRESHOLD, USER_ONLINE_THRESHOLD } from '@/const.js';
import { MemoryKVCache } from '@/misc/cache.js'; import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
import type { Instance } from '@/models/entities/Instance.js'; import type { Instance } from '@/models/entities/Instance.js';
import type { LocalUser, RemoteUser, User } from '@/models/entities/User.js'; import type { LocalUser, RemoteUser, User } from '@/models/entities/User.js';
import { birthdaySchema, descriptionSchema, localUsernameSchema, locationSchema, nameSchema, passwordSchema } from '@/models/entities/User.js'; import { birthdaySchema, descriptionSchema, localUsernameSchema, locationSchema, nameSchema, passwordSchema } from '@/models/entities/User.js';
@ -53,7 +53,7 @@ export class UserEntityService implements OnModuleInit {
private customEmojiService: CustomEmojiService; private customEmojiService: CustomEmojiService;
private antennaService: AntennaService; private antennaService: AntennaService;
private roleService: RoleService; private roleService: RoleService;
private userInstanceCache: MemoryKVCache<Instance | null>; private userInstanceCache: RedisKVCache<Instance | null>;
constructor( constructor(
private moduleRef: ModuleRef, private moduleRef: ModuleRef,
@ -119,7 +119,13 @@ export class UserEntityService implements OnModuleInit {
//private antennaService: AntennaService, //private antennaService: AntennaService,
//private roleService: RoleService, //private roleService: RoleService,
) { ) {
this.userInstanceCache = new MemoryKVCache<Instance | null>(1000 * 60 * 60 * 3); this.userInstanceCache = new RedisKVCache<Instance | null>(this.redisClient, 'userInstance', {
lifetime: 1000 * 60 * 60 * 24, // 24h
memoryCacheLifetime: 1000 * 60 * 30, // 30m
fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
toRedisConverter: (value) => JSON.stringify(value),
fromRedisConverter: (value) => JSON.parse(value), // TODO: date型の考慮
});
} }
onModuleInit() { onModuleInit() {
@ -343,10 +349,7 @@ export class UserEntityService implements OnModuleInit {
avatarBlurhash: user.avatarBlurhash, avatarBlurhash: user.avatarBlurhash,
isBot: user.isBot ?? falsy, isBot: user.isBot ?? falsy,
isCat: user.isCat ?? falsy, isCat: user.isCat ?? falsy,
instance: user.host ? this.userInstanceCache.fetch(user.host, instance: user.host ? this.userInstanceCache.fetch(user.host).then(instance => instance ? {
() => this.instancesRepository.findOneBy({ host: user.host! }),
v => v != null,
).then(instance => instance ? {
name: instance.name, name: instance.name,
softwareName: instance.softwareName, softwareName: instance.softwareName,
softwareVersion: instance.softwareVersion, softwareVersion: instance.softwareVersion,