diff --git a/packages/backend/src/core/CacheService.ts b/packages/backend/src/core/CacheService.ts index 1fba4f32d0..ae24a9721f 100644 --- a/packages/backend/src/core/CacheService.ts +++ b/packages/backend/src/core/CacheService.ts @@ -411,6 +411,62 @@ export class CacheService implements OnApplicationShutdown { return blockers; } + public async getUserProfiles(userIds: Iterable): Promise> { + const profiles = new Map; + + const toFetch: string[] = []; + for (const userId of userIds) { + const fromCache = this.userProfileCache.get(userId); + if (fromCache) { + profiles.set(userId, fromCache); + } else { + toFetch.push(userId); + } + } + + if (toFetch.length > 0) { + const fetched = await this.userProfilesRepository.findBy({ + userId: In(toFetch), + }); + + for (const profile of fetched) { + profiles.set(profile.userId, profile); + } + + const toCache = new Map(fetched.map(p => [p.userId, p])); + this.userProfileCache.addMany(toCache); + } + + return profiles; + } + + public async getUsers(userIds: Iterable): Promise> { + const users = new Map; + + const toFetch: string[] = []; + for (const userId of userIds) { + const fromCache = this.userByIdCache.get(userId); + if (fromCache) { + users.set(userId, fromCache); + } else { + toFetch.push(userId); + } + } + + if (toFetch.length > 0) { + const fetched = await this.usersRepository.findBy({ + id: In(toFetch), + }); + + for (const user of fetched) { + users.set(user.id, user); + this.userByIdCache.set(user.id, user); + } + } + + return users; + } + @bindThis public dispose(): void { this.internalEventService.off('userChangeSuspendedState', this.onUserEvent);