summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/UserCacheService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-04-04 17:32:09 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-04-04 17:32:09 +0900
commitecaf152b4a6eb702375debaef0dddc2cca798116 (patch)
tree85b2ca34a67ad66368fea84d0462b5c109b22415 /packages/backend/src/core/UserCacheService.ts
parentrefactor(backend): rename Cache -> MemoryCache (diff)
downloadsharkey-ecaf152b4a6eb702375debaef0dddc2cca798116.tar.gz
sharkey-ecaf152b4a6eb702375debaef0dddc2cca798116.tar.bz2
sharkey-ecaf152b4a6eb702375debaef0dddc2cca798116.zip
enhance(backend): improve cache
Diffstat (limited to 'packages/backend/src/core/UserCacheService.ts')
-rw-r--r--packages/backend/src/core/UserCacheService.ts88
1 files changed, 0 insertions, 88 deletions
diff --git a/packages/backend/src/core/UserCacheService.ts b/packages/backend/src/core/UserCacheService.ts
deleted file mode 100644
index e452caf5d1..0000000000
--- a/packages/backend/src/core/UserCacheService.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import { Inject, Injectable } from '@nestjs/common';
-import Redis from 'ioredis';
-import type { UsersRepository } from '@/models/index.js';
-import { MemoryKVCache } from '@/misc/cache.js';
-import type { LocalUser, User } from '@/models/entities/User.js';
-import { DI } from '@/di-symbols.js';
-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';
-
-@Injectable()
-export class UserCacheService implements OnApplicationShutdown {
- public userByIdCache: MemoryKVCache<User>;
- public localUserByNativeTokenCache: MemoryKVCache<LocalUser | null>;
- public localUserByIdCache: MemoryKVCache<LocalUser>;
- public uriPersonCache: MemoryKVCache<User | null>;
-
- constructor(
- @Inject(DI.redisSubscriber)
- private redisSubscriber: Redis.Redis,
-
- @Inject(DI.usersRepository)
- private usersRepository: UsersRepository,
-
- private userEntityService: UserEntityService,
- ) {
- //this.onMessage = this.onMessage.bind(this);
-
- 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);
-
- this.redisSubscriber.on('message', this.onMessage);
- }
-
- @bindThis
- private async onMessage(_: string, data: string): Promise<void> {
- const obj = JSON.parse(data);
-
- if (obj.channel === 'internal') {
- const { type, body } = obj.message as StreamMessages['internal']['payload'];
- switch (type) {
- case 'userChangeSuspendedState':
- case 'remoteUserUpdated': {
- const user = await this.usersRepository.findOneByOrFail({ id: body.id });
- this.userByIdCache.set(user.id, user);
- for (const [k, v] of this.uriPersonCache.cache.entries()) {
- if (v.value?.id === user.id) {
- this.uriPersonCache.set(k, user);
- }
- }
- if (this.userEntityService.isLocalUser(user)) {
- this.localUserByNativeTokenCache.set(user.token, user);
- this.localUserByIdCache.set(user.id, user);
- }
- break;
- }
- case 'userTokenRegenerated': {
- const user = await this.usersRepository.findOneByOrFail({ id: body.id }) as LocalUser;
- 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;
- }
- default:
- break;
- }
- }
- }
-
- @bindThis
- public findById(userId: User['id']) {
- return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId }));
- }
-
- @bindThis
- public onApplicationShutdown(signal?: string | undefined) {
- this.redisSubscriber.off('message', this.onMessage);
- }
-}