diff --git a/packages/backend/src/core/CustomEmojiService.ts b/packages/backend/src/core/CustomEmojiService.ts index a62854c61c6ddec9e4644e4ea12b8172b4f4173d..1c3b60e5d70f4cc7a29ba870a405e6c1e127e7d1 100644 --- a/packages/backend/src/core/CustomEmojiService.ts +++ b/packages/backend/src/core/CustomEmojiService.ts @@ -8,7 +8,7 @@ import type { DriveFile } from '@/models/entities/DriveFile.js'; import type { Emoji } from '@/models/entities/Emoji.js'; import type { EmojisRepository, Note } from '@/models/index.js'; import { bindThis } from '@/decorators.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import { UtilityService } from '@/core/UtilityService.js'; import type { Config } from '@/config.js'; import { ReactionService } from '@/core/ReactionService.js'; @@ -16,7 +16,7 @@ import { query } from '@/misc/prelude/url.js'; @Injectable() export class CustomEmojiService { - private cache: KVCache<Emoji | null>; + private cache: MemoryKVCache<Emoji | null>; constructor( @Inject(DI.config) @@ -34,7 +34,7 @@ export class CustomEmojiService { private globalEventService: GlobalEventService, private reactionService: ReactionService, ) { - this.cache = new KVCache<Emoji | null>(1000 * 60 * 60 * 12); + this.cache = new MemoryKVCache<Emoji | null>(1000 * 60 * 60 * 12); } @bindThis diff --git a/packages/backend/src/core/FederatedInstanceService.ts b/packages/backend/src/core/FederatedInstanceService.ts index b85791e43f12f37969cf08f1265eee6db957646f..2c6d3ac508a0bbb3022c68c3c5d11a6801e16d76 100644 --- a/packages/backend/src/core/FederatedInstanceService.ts +++ b/packages/backend/src/core/FederatedInstanceService.ts @@ -1,7 +1,7 @@ import { Inject, Injectable } from '@nestjs/common'; import type { InstancesRepository } from '@/models/index.js'; import type { Instance } from '@/models/entities/Instance.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import { IdService } from '@/core/IdService.js'; import { DI } from '@/di-symbols.js'; import { UtilityService } from '@/core/UtilityService.js'; @@ -9,7 +9,7 @@ import { bindThis } from '@/decorators.js'; @Injectable() export class FederatedInstanceService { - private cache: KVCache<Instance>; + private cache: MemoryKVCache<Instance>; constructor( @Inject(DI.instancesRepository) @@ -18,7 +18,7 @@ export class FederatedInstanceService { private utilityService: UtilityService, private idService: IdService, ) { - this.cache = new KVCache<Instance>(1000 * 60 * 60); + this.cache = new MemoryKVCache<Instance>(1000 * 60 * 60); } @bindThis diff --git a/packages/backend/src/core/InstanceActorService.ts b/packages/backend/src/core/InstanceActorService.ts index ef87051a74045b54904901a2cffeebc1dfe8b1a2..049e27dec858b9826885756eed9795bec43eab91 100644 --- a/packages/backend/src/core/InstanceActorService.ts +++ b/packages/backend/src/core/InstanceActorService.ts @@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'; import { IsNull } from 'typeorm'; import type { LocalUser } from '@/models/entities/User.js'; import type { UsersRepository } from '@/models/index.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import { DI } from '@/di-symbols.js'; import { CreateSystemUserService } from '@/core/CreateSystemUserService.js'; import { bindThis } from '@/decorators.js'; @@ -11,7 +11,7 @@ const ACTOR_USERNAME = 'instance.actor' as const; @Injectable() export class InstanceActorService { - private cache: KVCache<LocalUser>; + private cache: MemoryKVCache<LocalUser>; constructor( @Inject(DI.usersRepository) @@ -19,7 +19,7 @@ export class InstanceActorService { private createSystemUserService: CreateSystemUserService, ) { - this.cache = new KVCache<LocalUser>(Infinity); + this.cache = new MemoryKVCache<LocalUser>(Infinity); } @bindThis diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index 7af7099432a314989a7e009c9887707b3ab67ae6..552f241044eb9d90c916eb70f3a71c5e97f9f4ff 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -20,7 +20,7 @@ import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js import { checkWordMute } from '@/misc/check-word-mute.js'; import type { Channel } from '@/models/entities/Channel.js'; import { normalizeForSearch } from '@/misc/normalize-for-search.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { UserProfile } from '@/models/entities/UserProfile.js'; import { RelayService } from '@/core/RelayService.js'; import { FederatedInstanceService } from '@/core/FederatedInstanceService.js'; @@ -47,7 +47,7 @@ import { DB_MAX_NOTE_TEXT_LENGTH } from '@/const.js'; import { RoleService } from '@/core/RoleService.js'; import { MetaService } from '@/core/MetaService.js'; -const mutedWordsCache = new KVCache<{ userId: UserProfile['userId']; mutedWords: UserProfile['mutedWords']; }[]>(1000 * 60 * 5); +const mutedWordsCache = new MemoryKVCache<{ userId: UserProfile['userId']; mutedWords: UserProfile['mutedWords']; }[]>(1000 * 60 * 5); type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; diff --git a/packages/backend/src/core/RelayService.ts b/packages/backend/src/core/RelayService.ts index 4537f1b81a53bb76c6b054067e1f3a272c1a2165..be5a4d4b024a3d00e45bd0afc62d4bf1d7d569b7 100644 --- a/packages/backend/src/core/RelayService.ts +++ b/packages/backend/src/core/RelayService.ts @@ -3,7 +3,7 @@ import { IsNull } from 'typeorm'; import type { LocalUser, User } from '@/models/entities/User.js'; import type { RelaysRepository, UsersRepository } from '@/models/index.js'; import { IdService } from '@/core/IdService.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { Relay } from '@/models/entities/Relay.js'; import { QueueService } from '@/core/QueueService.js'; import { CreateSystemUserService } from '@/core/CreateSystemUserService.js'; @@ -16,7 +16,7 @@ const ACTOR_USERNAME = 'relay.actor' as const; @Injectable() export class RelayService { - private relaysCache: KVCache<Relay[]>; + private relaysCache: MemoryKVCache<Relay[]>; constructor( @Inject(DI.usersRepository) @@ -30,7 +30,7 @@ export class RelayService { private createSystemUserService: CreateSystemUserService, private apRendererService: ApRendererService, ) { - this.relaysCache = new KVCache<Relay[]>(1000 * 60 * 10); + this.relaysCache = new MemoryKVCache<Relay[]>(1000 * 60 * 10); } @bindThis diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts index 7b63e43cb186e18b25c431fced9dbadfc038515c..678bcfc337cbae33db37d9f8be2c99790512f8bd 100644 --- a/packages/backend/src/core/RoleService.ts +++ b/packages/backend/src/core/RoleService.ts @@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'; import Redis from 'ioredis'; import { In } from 'typeorm'; import type { Role, RoleAssignment, RoleAssignmentsRepository, RolesRepository, UsersRepository } from '@/models/index.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { User } from '@/models/entities/User.js'; import { DI } from '@/di-symbols.js'; import { bindThis } from '@/decorators.js'; @@ -57,8 +57,8 @@ export const DEFAULT_POLICIES: RolePolicies = { @Injectable() export class RoleService implements OnApplicationShutdown { - private rolesCache: KVCache<Role[]>; - private roleAssignmentByUserIdCache: KVCache<RoleAssignment[]>; + private rolesCache: MemoryKVCache<Role[]>; + private roleAssignmentByUserIdCache: MemoryKVCache<RoleAssignment[]>; public static AlreadyAssignedError = class extends Error {}; public static NotAssignedError = class extends Error {}; @@ -84,8 +84,8 @@ export class RoleService implements OnApplicationShutdown { ) { //this.onMessage = this.onMessage.bind(this); - this.rolesCache = new KVCache<Role[]>(Infinity); - this.roleAssignmentByUserIdCache = new KVCache<RoleAssignment[]>(Infinity); + this.rolesCache = new MemoryKVCache<Role[]>(Infinity); + this.roleAssignmentByUserIdCache = new MemoryKVCache<RoleAssignment[]>(Infinity); this.redisSubscriber.on('message', this.onMessage); } diff --git a/packages/backend/src/core/UserBlockingService.ts b/packages/backend/src/core/UserBlockingService.ts index 33b51537a668650c19b0260a0a2c9cf181e027bc..040b6de2efb6f78626eb6a477da22d3e181dd3c2 100644 --- a/packages/backend/src/core/UserBlockingService.ts +++ b/packages/backend/src/core/UserBlockingService.ts @@ -15,7 +15,7 @@ import { ApRendererService } from '@/core/activitypub/ApRendererService.js'; import { LoggerService } from '@/core/LoggerService.js'; import { WebhookService } from '@/core/WebhookService.js'; import { bindThis } from '@/decorators.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import { StreamMessages } from '@/server/api/stream/types.js'; @Injectable() @@ -23,7 +23,7 @@ export class UserBlockingService implements OnApplicationShutdown { private logger: Logger; // ã‚ーãŒãƒ¦ãƒ¼ã‚¶ãƒ¼IDã§ã€å€¤ãŒãã®ãƒ¦ãƒ¼ã‚¶ãƒ¼ãŒãƒ–ãƒãƒƒã‚¯ã—ã¦ã„るユーザーã®IDã®ãƒªã‚¹ãƒˆãªã‚ャッシュ - private blockingsByUserIdCache: KVCache<User['id'][]>; + private blockingsByUserIdCache: MemoryKVCache<User['id'][]>; constructor( @Inject(DI.redisSubscriber) @@ -58,7 +58,7 @@ export class UserBlockingService implements OnApplicationShutdown { ) { this.logger = this.loggerService.getLogger('user-block'); - this.blockingsByUserIdCache = new KVCache<User['id'][]>(Infinity); + this.blockingsByUserIdCache = new MemoryKVCache<User['id'][]>(Infinity); this.redisSubscriber.on('message', this.onMessage); } diff --git a/packages/backend/src/core/UserCacheService.ts b/packages/backend/src/core/UserCacheService.ts index 631eb44062ccb65d1cc5a2c32cbe54db33a36b79..e452caf5d128bd284f0bf8b0b459e0b9c59a244f 100644 --- a/packages/backend/src/core/UserCacheService.ts +++ b/packages/backend/src/core/UserCacheService.ts @@ -1,7 +1,7 @@ import { Inject, Injectable } from '@nestjs/common'; import Redis from 'ioredis'; import type { UsersRepository } from '@/models/index.js'; -import { KVCache } from '@/misc/cache.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'; @@ -11,10 +11,10 @@ import type { OnApplicationShutdown } from '@nestjs/common'; @Injectable() export class UserCacheService implements OnApplicationShutdown { - public userByIdCache: KVCache<User>; - public localUserByNativeTokenCache: KVCache<LocalUser | null>; - public localUserByIdCache: KVCache<LocalUser>; - public uriPersonCache: KVCache<User | null>; + public userByIdCache: MemoryKVCache<User>; + public localUserByNativeTokenCache: MemoryKVCache<LocalUser | null>; + public localUserByIdCache: MemoryKVCache<LocalUser>; + public uriPersonCache: MemoryKVCache<User | null>; constructor( @Inject(DI.redisSubscriber) @@ -27,10 +27,10 @@ export class UserCacheService implements OnApplicationShutdown { ) { //this.onMessage = this.onMessage.bind(this); - this.userByIdCache = new KVCache<User>(Infinity); - this.localUserByNativeTokenCache = new KVCache<LocalUser | null>(Infinity); - this.localUserByIdCache = new KVCache<LocalUser>(Infinity); - this.uriPersonCache = new KVCache<User | null>(Infinity); + 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); } diff --git a/packages/backend/src/core/UserKeypairStoreService.ts b/packages/backend/src/core/UserKeypairStoreService.ts index 61c9293f86cd3305d0fcb4c9e557aabf817d93a1..872a0335eabc7e7c83fb2cd9208e61c013f568e9 100644 --- a/packages/backend/src/core/UserKeypairStoreService.ts +++ b/packages/backend/src/core/UserKeypairStoreService.ts @@ -1,20 +1,20 @@ import { Inject, Injectable } from '@nestjs/common'; import type { User } from '@/models/entities/User.js'; import type { UserKeypairsRepository } from '@/models/index.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { UserKeypair } from '@/models/entities/UserKeypair.js'; import { DI } from '@/di-symbols.js'; import { bindThis } from '@/decorators.js'; @Injectable() export class UserKeypairStoreService { - private cache: KVCache<UserKeypair>; + private cache: MemoryKVCache<UserKeypair>; constructor( @Inject(DI.userKeypairsRepository) private userKeypairsRepository: UserKeypairsRepository, ) { - this.cache = new KVCache<UserKeypair>(Infinity); + this.cache = new MemoryKVCache<UserKeypair>(Infinity); } @bindThis diff --git a/packages/backend/src/core/activitypub/ApDbResolverService.ts b/packages/backend/src/core/activitypub/ApDbResolverService.ts index c3b3875613f854ccd2b728f60020963f44def4cc..dc0a865bbeb5c832f4beab1a1a2b100cb6a5f34d 100644 --- a/packages/backend/src/core/activitypub/ApDbResolverService.ts +++ b/packages/backend/src/core/activitypub/ApDbResolverService.ts @@ -3,7 +3,7 @@ import escapeRegexp from 'escape-regexp'; import { DI } from '@/di-symbols.js'; import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js'; import type { Config } from '@/config.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { UserPublickey } from '@/models/entities/UserPublickey.js'; import { UserCacheService } from '@/core/UserCacheService.js'; import type { Note } from '@/models/entities/Note.js'; @@ -31,8 +31,8 @@ export type UriParseResult = { @Injectable() export class ApDbResolverService { - private publicKeyCache: KVCache<UserPublickey | null>; - private publicKeyByUserIdCache: KVCache<UserPublickey | null>; + private publicKeyCache: MemoryKVCache<UserPublickey | null>; + private publicKeyByUserIdCache: MemoryKVCache<UserPublickey | null>; constructor( @Inject(DI.config) @@ -50,8 +50,8 @@ export class ApDbResolverService { private userCacheService: UserCacheService, private apPersonService: ApPersonService, ) { - this.publicKeyCache = new KVCache<UserPublickey | null>(Infinity); - this.publicKeyByUserIdCache = new KVCache<UserPublickey | null>(Infinity); + this.publicKeyCache = new MemoryKVCache<UserPublickey | null>(Infinity); + this.publicKeyByUserIdCache = new MemoryKVCache<UserPublickey | null>(Infinity); } @bindThis diff --git a/packages/backend/src/core/entities/UserEntityService.ts b/packages/backend/src/core/entities/UserEntityService.ts index ae7c47a990b06091f5d35e2378cd30a83805ac0b..71aa2ee6de71972063e01d69094d7cf759ca02f9 100644 --- a/packages/backend/src/core/entities/UserEntityService.ts +++ b/packages/backend/src/core/entities/UserEntityService.ts @@ -9,7 +9,7 @@ import type { Packed } from '@/misc/json-schema.js'; import type { Promiseable } 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 { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { Instance } from '@/models/entities/Instance.js'; import type { LocalUser, RemoteUser, User } 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 antennaService: AntennaService; private roleService: RoleService; - private userInstanceCache: KVCache<Instance | null>; + private userInstanceCache: MemoryKVCache<Instance | null>; constructor( private moduleRef: ModuleRef, @@ -119,7 +119,7 @@ export class UserEntityService implements OnModuleInit { //private antennaService: AntennaService, //private roleService: RoleService, ) { - this.userInstanceCache = new KVCache<Instance | null>(1000 * 60 * 60 * 3); + this.userInstanceCache = new MemoryKVCache<Instance | null>(1000 * 60 * 60 * 3); } onModuleInit() { diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index b249cf448055d23619fe90e4d5cbbe39a9d54e92..a805d18421a31190f9ebb56a939960df8a39f3a6 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -2,11 +2,11 @@ import { bindThis } from '@/decorators.js'; // TODO: メモリ節約ã®ãŸã‚ã‚ã¾ã‚Šå‚ç…§ã•ã‚Œãªã„ã‚ーを定期的ã«å‰Šé™¤ã§ãるよã†ã«ã™ã‚‹ï¼Ÿ -export class KVCache<T> { +export class MemoryKVCache<T> { public cache: Map<string | null, { date: number; value: T; }>; private lifetime: number; - constructor(lifetime: KVCache<never>['lifetime']) { + constructor(lifetime: MemoryKVCache<never>['lifetime']) { this.cache = new Map(); this.lifetime = lifetime; } @@ -88,12 +88,12 @@ export class KVCache<T> { } } -export class Cache<T> { +export class MemoryCache<T> { private cachedAt: number | null = null; private value: T | undefined; private lifetime: number; - constructor(lifetime: Cache<never>['lifetime']) { + constructor(lifetime: MemoryCache<never>['lifetime']) { this.lifetime = lifetime; } diff --git a/packages/backend/src/queue/processors/DeliverProcessorService.ts b/packages/backend/src/queue/processors/DeliverProcessorService.ts index f637bf88188ffdcf18eaeed3b115fbcbc3e6585d..71865c778ad4f8337b9dae09bfd8ba08708606f7 100644 --- a/packages/backend/src/queue/processors/DeliverProcessorService.ts +++ b/packages/backend/src/queue/processors/DeliverProcessorService.ts @@ -7,7 +7,7 @@ import { MetaService } from '@/core/MetaService.js'; import { ApRequestService } from '@/core/activitypub/ApRequestService.js'; import { FederatedInstanceService } from '@/core/FederatedInstanceService.js'; import { FetchInstanceMetadataService } from '@/core/FetchInstanceMetadataService.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { Instance } from '@/models/entities/Instance.js'; import InstanceChart from '@/core/chart/charts/instance.js'; import ApRequestChart from '@/core/chart/charts/ap-request.js'; @@ -22,7 +22,7 @@ import type { DeliverJobData } from '../types.js'; @Injectable() export class DeliverProcessorService { private logger: Logger; - private suspendedHostsCache: KVCache<Instance[]>; + private suspendedHostsCache: MemoryKVCache<Instance[]>; private latest: string | null; constructor( @@ -46,7 +46,7 @@ export class DeliverProcessorService { private queueLoggerService: QueueLoggerService, ) { this.logger = this.queueLoggerService.logger.createSubLogger('deliver'); - this.suspendedHostsCache = new KVCache<Instance[]>(1000 * 60 * 60); + this.suspendedHostsCache = new MemoryKVCache<Instance[]>(1000 * 60 * 60); } @bindThis diff --git a/packages/backend/src/server/NodeinfoServerService.ts b/packages/backend/src/server/NodeinfoServerService.ts index 86019d4166c39744e31b410fc1e934ea5c7c5113..3387bd53aa02a5ecbc43baf2829b1babb3e53907 100644 --- a/packages/backend/src/server/NodeinfoServerService.ts +++ b/packages/backend/src/server/NodeinfoServerService.ts @@ -4,7 +4,7 @@ import type { NotesRepository, UsersRepository } from '@/models/index.js'; import type { Config } from '@/config.js'; import { MetaService } from '@/core/MetaService.js'; import { MAX_NOTE_TEXT_LENGTH } from '@/const.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { bindThis } from '@/decorators.js'; import NotesChart from '@/core/chart/charts/notes.js'; @@ -118,7 +118,7 @@ export class NodeinfoServerService { }; }; - const cache = new KVCache<Awaited<ReturnType<typeof nodeinfo2>>>(1000 * 60 * 10); + const cache = new MemoryKVCache<Awaited<ReturnType<typeof nodeinfo2>>>(1000 * 60 * 10); fastify.get(nodeinfo2_1path, async (request, reply) => { const base = await cache.fetch(null, () => nodeinfo2()); diff --git a/packages/backend/src/server/api/AuthenticateService.ts b/packages/backend/src/server/api/AuthenticateService.ts index a1895e3705661d672ea4eb41b755135b8441ce9d..cd6bce9ef9a9221ea81849b531d4b0e58651b10b 100644 --- a/packages/backend/src/server/api/AuthenticateService.ts +++ b/packages/backend/src/server/api/AuthenticateService.ts @@ -3,7 +3,7 @@ import { DI } from '@/di-symbols.js'; import type { AccessTokensRepository, AppsRepository, UsersRepository } from '@/models/index.js'; import type { LocalUser } from '@/models/entities/User.js'; import type { AccessToken } from '@/models/entities/AccessToken.js'; -import { KVCache } from '@/misc/cache.js'; +import { MemoryKVCache } from '@/misc/cache.js'; import type { App } from '@/models/entities/App.js'; import { UserCacheService } from '@/core/UserCacheService.js'; import isNativeToken from '@/misc/is-native-token.js'; @@ -18,7 +18,7 @@ export class AuthenticationError extends Error { @Injectable() export class AuthenticateService { - private appCache: KVCache<App>; + private appCache: MemoryKVCache<App>; constructor( @Inject(DI.usersRepository) @@ -32,7 +32,7 @@ export class AuthenticateService { private userCacheService: UserCacheService, ) { - this.appCache = new KVCache<App>(Infinity); + this.appCache = new MemoryKVCache<App>(Infinity); } @bindThis