misskey/packages/backend/src/core/CacheService.ts

96 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';
2023-04-04 08:32:09 +00:00
import type { UserProfile, UsersRepository } from '@/models/index.js';
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
2023-02-13 06:50:22 +00:00
import type { LocalUser, User } from '@/models/entities/User.js';
2022-09-17 18:27:08 +00:00
import { DI } from '@/di-symbols.js';
2022-12-04 01:16:03 +00:00
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { bindThis } from '@/decorators.js';
import { StreamMessages } from '@/server/api/stream/types.js';
import type { OnApplicationShutdown } from '@nestjs/common';
2022-09-17 18:27:08 +00:00
@Injectable()
2023-04-04 08:32:09 +00:00
export class CacheService implements OnApplicationShutdown {
public userByIdCache: MemoryKVCache<User>;
public localUserByNativeTokenCache: MemoryKVCache<LocalUser | null>;
public localUserByIdCache: MemoryKVCache<LocalUser>;
public uriPersonCache: MemoryKVCache<User | null>;
2023-04-04 08:32:09 +00:00
public userProfileCache: RedisKVCache<UserProfile>;
public userMutingsCache: RedisKVCache<string[]>;
2022-09-17 18:27:08 +00:00
constructor(
2023-04-04 08:32:09 +00:00
@Inject(DI.redis)
private redisClient: Redis.Redis,
2022-09-17 18:27:08 +00:00
@Inject(DI.redisSubscriber)
private redisSubscriber: Redis.Redis,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
private userEntityService: UserEntityService,
) {
//this.onMessage = this.onMessage.bind(this);
2022-09-17 18:27:08 +00:00
this.userByIdCache = new MemoryKVCache<User>(Infinity);
this.localUserByNativeTokenCache = new MemoryKVCache<LocalUser | null>(Infinity);
this.localUserByIdCache = new MemoryKVCache<LocalUser>(Infinity);
this.uriPersonCache = new MemoryKVCache<User | null>(Infinity);
2023-04-04 08:32:09 +00:00
this.userProfileCache = new RedisKVCache<UserProfile>(this.redisClient, 'userProfile', 1000 * 60 * 60 * 24, 1000 * 60);
this.userMutingsCache = new RedisKVCache<string[]>(this.redisClient, 'userMutings', 1000 * 60 * 60 * 24, 1000 * 60);
2022-09-17 18:27:08 +00:00
this.redisSubscriber.on('message', this.onMessage);
}
@bindThis
2022-09-23 22:12:11 +00:00
private async onMessage(_: string, data: string): Promise<void> {
2022-09-17 18:27:08 +00:00
const obj = JSON.parse(data);
if (obj.channel === 'internal') {
const { type, body } = obj.message as StreamMessages['internal']['payload'];
2022-09-17 18:27:08 +00:00
switch (type) {
case 'userChangeSuspendedState':
case 'remoteUserUpdated': {
const user = await this.usersRepository.findOneByOrFail({ id: body.id });
2023-02-13 06:28:07 +00:00
this.userByIdCache.set(user.id, user);
2022-09-17 18:27:08 +00:00
for (const [k, v] of this.uriPersonCache.cache.entries()) {
if (v.value?.id === user.id) {
2023-02-13 06:28:07 +00:00
this.uriPersonCache.set(k, user);
2022-09-17 18:27:08 +00:00
}
}
if (this.userEntityService.isLocalUser(user)) {
2023-04-04 08:32:09 +00:00
this.localUserByNativeTokenCache.set(user.token!, user);
2022-09-17 18:27:08 +00:00
this.localUserByIdCache.set(user.id, user);
}
break;
}
case 'userTokenRegenerated': {
2023-02-13 06:50:22 +00:00
const user = await this.usersRepository.findOneByOrFail({ id: body.id }) as LocalUser;
2022-09-17 18:27:08 +00:00
this.localUserByNativeTokenCache.delete(body.oldToken);
this.localUserByNativeTokenCache.set(body.newToken, user);
break;
}
case 'follow': {
const follower = this.userByIdCache.get(body.followerId);
if (follower) follower.followingCount++;
const followee = this.userByIdCache.get(body.followeeId);
if (followee) followee.followersCount++;
break;
}
2022-09-17 18:27:08 +00:00
default:
break;
}
}
}
2023-01-12 23:56:06 +00:00
@bindThis
2023-04-04 08:32:09 +00:00
public findUserById(userId: User['id']) {
2023-02-13 06:28:07 +00:00
return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId }));
2023-01-12 23:56:06 +00:00
}
@bindThis
2022-09-17 18:27:08 +00:00
public onApplicationShutdown(signal?: string | undefined) {
this.redisSubscriber.off('message', this.onMessage);
}
}