diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2023-04-07 18:48:45 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2023-04-07 18:48:45 +0900 |
| commit | 1377ea4178ffb49cc76466df56ef945fc4c2f713 (patch) | |
| tree | 6b56cb816ec536c56cd5bf09ce240dab1b307f79 /packages/backend/src/core/FederatedInstanceService.ts | |
| parent | enhance(backend): tweak cache of role (diff) | |
| download | sharkey-1377ea4178ffb49cc76466df56ef945fc4c2f713.tar.gz sharkey-1377ea4178ffb49cc76466df56ef945fc4c2f713.tar.bz2 sharkey-1377ea4178ffb49cc76466df56ef945fc4c2f713.zip | |
perf(backend): improve cache of federated instances
Diffstat (limited to 'packages/backend/src/core/FederatedInstanceService.ts')
| -rw-r--r-- | packages/backend/src/core/FederatedInstanceService.ts | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/packages/backend/src/core/FederatedInstanceService.ts b/packages/backend/src/core/FederatedInstanceService.ts index 2c6d3ac508..bc66591bc9 100644 --- a/packages/backend/src/core/FederatedInstanceService.ts +++ b/packages/backend/src/core/FederatedInstanceService.ts @@ -1,7 +1,8 @@ import { Inject, Injectable } from '@nestjs/common'; +import Redis from 'ioredis'; import type { InstancesRepository } from '@/models/index.js'; import type { Instance } from '@/models/entities/Instance.js'; -import { MemoryKVCache } from '@/misc/cache.js'; +import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js'; import { IdService } from '@/core/IdService.js'; import { DI } from '@/di-symbols.js'; import { UtilityService } from '@/core/UtilityService.js'; @@ -9,23 +10,32 @@ import { bindThis } from '@/decorators.js'; @Injectable() export class FederatedInstanceService { - private cache: MemoryKVCache<Instance>; + public federatedInstanceCache: RedisKVCache<Instance | null>; constructor( + @Inject(DI.redis) + private redisClient: Redis.Redis, + @Inject(DI.instancesRepository) private instancesRepository: InstancesRepository, private utilityService: UtilityService, private idService: IdService, ) { - this.cache = new MemoryKVCache<Instance>(1000 * 60 * 60); + this.federatedInstanceCache = new RedisKVCache<Instance | null>(this.redisClient, 'federatedInstance', { + lifetime: 1000 * 60 * 60 * 24, // 24h + memoryCacheLifetime: 1000 * 60 * 30, // 30m + fetcher: (key) => this.instancesRepository.findOneBy({ host: key }), + toRedisConverter: (value) => JSON.stringify(value), + fromRedisConverter: (value) => JSON.parse(value), // TODO: date型の考慮 + }); } @bindThis public async fetch(host: string): Promise<Instance> { host = this.utilityService.toPuny(host); - const cached = this.cache.get(host); + const cached = await this.federatedInstanceCache.get(host); if (cached) return cached; const index = await this.instancesRepository.findOneBy({ host }); @@ -37,10 +47,10 @@ export class FederatedInstanceService { firstRetrievedAt: new Date(), }).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0])); - this.cache.set(host, i); + this.federatedInstanceCache.set(host, i); return i; } else { - this.cache.set(host, index); + this.federatedInstanceCache.set(host, index); return index; } } @@ -49,10 +59,10 @@ export class FederatedInstanceService { public async updateCachePartial(host: string, data: Partial<Instance>): Promise<void> { host = this.utilityService.toPuny(host); - const cached = this.cache.get(host); + const cached = await this.federatedInstanceCache.get(host); if (cached == null) return; - this.cache.set(host, { + this.federatedInstanceCache.set(host, { ...cached, ...data, }); |