2023-07-27 00:31:52 -05:00
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-13 23:50:05 -05:00
|
|
|
import * as Redis from 'ioredis';
|
2023-10-27 04:34:02 -05:00
|
|
|
import type { BlockingsRepository, FollowingsRepository, MutingsRepository, RenoteMutingsRepository, MiUserProfile, UserProfilesRepository, UsersRepository, MiFollowing } from '@/models/_.js';
|
2023-04-04 03:32:09 -05:00
|
|
|
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiLocalUser, MiUser } from '@/models/User.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-03 19:16:03 -06:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-09-28 21:29:54 -05:00
|
|
|
import type { GlobalEvents } from '@/core/GlobalEventService.js';
|
2023-01-12 06:02:26 -06:00
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Injectable()
|
2023-04-04 03:32:09 -05:00
|
|
|
export class CacheService implements OnApplicationShutdown {
|
2024-02-16 00:07:12 -06:00
|
|
|
public userByIdCache: MemoryKVCache<MiUser>;
|
|
|
|
public localUserByNativeTokenCache: MemoryKVCache<MiLocalUser | null>;
|
2023-08-16 03:51:28 -05:00
|
|
|
public localUserByIdCache: MemoryKVCache<MiLocalUser>;
|
2024-02-16 00:07:12 -06:00
|
|
|
public uriPersonCache: MemoryKVCache<MiUser | null>;
|
2023-08-16 03:51:28 -05:00
|
|
|
public userProfileCache: RedisKVCache<MiUserProfile>;
|
2023-04-04 20:21:10 -05:00
|
|
|
public userMutingsCache: RedisKVCache<Set<string>>;
|
|
|
|
public userBlockingCache: RedisKVCache<Set<string>>;
|
|
|
|
public userBlockedCache: RedisKVCache<Set<string>>; // NOTE: 「被」Blockキャッシュ
|
|
|
|
public renoteMutingsCache: RedisKVCache<Set<string>>;
|
2023-10-03 06:26:11 -05:00
|
|
|
public userFollowingsCache: RedisKVCache<Record<string, Pick<MiFollowing, 'withReplies'> | undefined>>;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
constructor(
|
2023-04-04 03:32:09 -05:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2023-04-09 03:09:27 -05:00
|
|
|
@Inject(DI.redisForSub)
|
|
|
|
private redisForSub: Redis.Redis,
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
2023-04-04 20:21:10 -05:00
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.blockingsRepository)
|
|
|
|
private blockingsRepository: BlockingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.renoteMutingsRepository)
|
|
|
|
private renoteMutingsRepository: RenoteMutingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
2022-12-04 00:03:09 -06:00
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2024-02-16 00:07:12 -06:00
|
|
|
this.userByIdCache = new MemoryKVCache<MiUser>(Infinity);
|
|
|
|
this.localUserByNativeTokenCache = new MemoryKVCache<MiLocalUser | null>(Infinity);
|
|
|
|
this.localUserByIdCache = new MemoryKVCache<MiLocalUser>(Infinity);
|
|
|
|
this.uriPersonCache = new MemoryKVCache<MiUser | null>(Infinity);
|
2023-04-04 20:21:10 -05:00
|
|
|
|
2023-08-16 03:51:28 -05:00
|
|
|
this.userProfileCache = new RedisKVCache<MiUserProfile>(this.redisClient, 'userProfile', {
|
2023-04-04 20:21:10 -05:00
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60, // 1m
|
|
|
|
fetcher: (key) => this.userProfilesRepository.findOneByOrFail({ userId: key }),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(value),
|
|
|
|
fromRedisConverter: (value) => JSON.parse(value), // TODO: date型の考慮
|
|
|
|
});
|
|
|
|
|
|
|
|
this.userMutingsCache = new RedisKVCache<Set<string>>(this.redisClient, 'userMutings', {
|
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60, // 1m
|
|
|
|
fetcher: (key) => this.mutingsRepository.find({ where: { muterId: key }, select: ['muteeId'] }).then(xs => new Set(xs.map(x => x.muteeId))),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(Array.from(value)),
|
|
|
|
fromRedisConverter: (value) => new Set(JSON.parse(value)),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.userBlockingCache = new RedisKVCache<Set<string>>(this.redisClient, 'userBlocking', {
|
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60, // 1m
|
|
|
|
fetcher: (key) => this.blockingsRepository.find({ where: { blockerId: key }, select: ['blockeeId'] }).then(xs => new Set(xs.map(x => x.blockeeId))),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(Array.from(value)),
|
|
|
|
fromRedisConverter: (value) => new Set(JSON.parse(value)),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.userBlockedCache = new RedisKVCache<Set<string>>(this.redisClient, 'userBlocked', {
|
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60, // 1m
|
|
|
|
fetcher: (key) => this.blockingsRepository.find({ where: { blockeeId: key }, select: ['blockerId'] }).then(xs => new Set(xs.map(x => x.blockerId))),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(Array.from(value)),
|
|
|
|
fromRedisConverter: (value) => new Set(JSON.parse(value)),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.renoteMutingsCache = new RedisKVCache<Set<string>>(this.redisClient, 'renoteMutings', {
|
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60, // 1m
|
|
|
|
fetcher: (key) => this.renoteMutingsRepository.find({ where: { muterId: key }, select: ['muteeId'] }).then(xs => new Set(xs.map(x => x.muteeId))),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(Array.from(value)),
|
|
|
|
fromRedisConverter: (value) => new Set(JSON.parse(value)),
|
|
|
|
});
|
|
|
|
|
2023-10-03 06:26:11 -05:00
|
|
|
this.userFollowingsCache = new RedisKVCache<Record<string, Pick<MiFollowing, 'withReplies'> | undefined>>(this.redisClient, 'userFollowings', {
|
2023-04-04 20:21:10 -05:00
|
|
|
lifetime: 1000 * 60 * 30, // 30m
|
|
|
|
memoryCacheLifetime: 1000 * 60, // 1m
|
2023-10-03 06:26:11 -05:00
|
|
|
fetcher: (key) => this.followingsRepository.find({ where: { followerId: key }, select: ['followeeId', 'withReplies'] }).then(xs => {
|
|
|
|
const obj: Record<string, Pick<MiFollowing, 'withReplies'> | undefined> = {};
|
|
|
|
for (const x of xs) {
|
|
|
|
obj[x.followeeId] = { withReplies: x.withReplies };
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(value),
|
|
|
|
fromRedisConverter: (value) => JSON.parse(value),
|
2023-04-04 20:21:10 -05:00
|
|
|
});
|
|
|
|
|
2023-10-27 04:34:02 -05:00
|
|
|
// NOTE: チャンネルのフォロー状況キャッシュはChannelFollowingServiceで行っている
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2023-04-09 03:09:27 -05:00
|
|
|
this.redisForSub.on('message', this.onMessage);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-23 17:12:11 -05:00
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
2022-09-17 13:27:08 -05:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-09-28 21:29:54 -05:00
|
|
|
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
|
2022-09-17 13:27:08 -05:00
|
|
|
switch (type) {
|
|
|
|
case 'userChangeSuspendedState':
|
|
|
|
case 'remoteUserUpdated': {
|
2024-02-15 23:30:53 -06:00
|
|
|
const user = await this.usersRepository.findOneBy({ id: body.id });
|
|
|
|
if (user == null) {
|
|
|
|
this.userByIdCache.delete(body.id);
|
|
|
|
for (const [k, v] of this.uriPersonCache.cache.entries()) {
|
2024-02-16 00:09:05 -06:00
|
|
|
if (v.value?.id === body.id) {
|
2024-02-15 23:30:53 -06:00
|
|
|
this.uriPersonCache.delete(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.userByIdCache.set(user.id, user);
|
|
|
|
for (const [k, v] of this.uriPersonCache.cache.entries()) {
|
2024-02-16 00:09:05 -06:00
|
|
|
if (v.value?.id === user.id) {
|
2024-02-15 23:30:53 -06:00
|
|
|
this.uriPersonCache.set(k, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.userEntityService.isLocalUser(user)) {
|
|
|
|
this.localUserByNativeTokenCache.set(user.token!, user);
|
|
|
|
this.localUserByIdCache.set(user.id, user);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'userTokenRegenerated': {
|
2023-08-16 03:51:28 -05:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: body.id }) as MiLocalUser;
|
2022-09-17 13:27:08 -05:00
|
|
|
this.localUserByNativeTokenCache.delete(body.oldToken);
|
|
|
|
this.localUserByNativeTokenCache.set(body.newToken, user);
|
|
|
|
break;
|
|
|
|
}
|
2023-01-13 17:27:23 -06:00
|
|
|
case 'follow': {
|
|
|
|
const follower = this.userByIdCache.get(body.followerId);
|
|
|
|
if (follower) follower.followingCount++;
|
|
|
|
const followee = this.userByIdCache.get(body.followeeId);
|
|
|
|
if (followee) followee.followersCount++;
|
2023-10-03 06:26:11 -05:00
|
|
|
this.userFollowingsCache.delete(body.followerId);
|
2023-01-13 17:27:23 -06:00
|
|
|
break;
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:56:06 -06:00
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
public findUserById(userId: MiUser['id']) {
|
2023-02-13 00:28:07 -06:00
|
|
|
return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId }));
|
2023-01-12 17:56:06 -06:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-05-28 23:21:26 -05:00
|
|
|
public dispose(): void {
|
2023-04-09 03:09:27 -05:00
|
|
|
this.redisForSub.off('message', this.onMessage);
|
2023-06-09 23:45:11 -05:00
|
|
|
this.userByIdCache.dispose();
|
|
|
|
this.localUserByNativeTokenCache.dispose();
|
|
|
|
this.localUserByIdCache.dispose();
|
|
|
|
this.uriPersonCache.dispose();
|
|
|
|
this.userProfileCache.dispose();
|
|
|
|
this.userMutingsCache.dispose();
|
|
|
|
this.userBlockingCache.dispose();
|
|
|
|
this.userBlockedCache.dispose();
|
|
|
|
this.renoteMutingsCache.dispose();
|
|
|
|
this.userFollowingsCache.dispose();
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
2023-05-28 23:21:26 -05:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|