summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/UserKeypairService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-04-05 13:50:05 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-04-05 13:50:05 +0900
commit288cce4a22186bf5dae98ed88dd737d21773c568 (patch)
tree29719102579ebf983e2a0950e309e317e703757e /packages/backend/src/core/UserKeypairService.ts
parentenhance(backend): improve userkeypair cache (diff)
downloadsharkey-288cce4a22186bf5dae98ed88dd737d21773c568.tar.gz
sharkey-288cce4a22186bf5dae98ed88dd737d21773c568.tar.bz2
sharkey-288cce4a22186bf5dae98ed88dd737d21773c568.zip
fix
Diffstat (limited to 'packages/backend/src/core/UserKeypairService.ts')
-rw-r--r--packages/backend/src/core/UserKeypairService.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/backend/src/core/UserKeypairService.ts b/packages/backend/src/core/UserKeypairService.ts
new file mode 100644
index 0000000000..22a9fb2b8e
--- /dev/null
+++ b/packages/backend/src/core/UserKeypairService.ts
@@ -0,0 +1,34 @@
+import { Inject, Injectable } from '@nestjs/common';
+import Redis from 'ioredis';
+import type { User } from '@/models/entities/User.js';
+import type { UserKeypairsRepository } from '@/models/index.js';
+import { RedisKVCache } 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 UserKeypairService {
+ private cache: RedisKVCache<UserKeypair>;
+
+ constructor(
+ @Inject(DI.redis)
+ private redisClient: Redis.Redis,
+
+ @Inject(DI.userKeypairsRepository)
+ private userKeypairsRepository: UserKeypairsRepository,
+ ) {
+ this.cache = new RedisKVCache<UserKeypair>(this.redisClient, 'userKeypair', {
+ lifetime: 1000 * 60 * 60 * 24, // 24h
+ memoryCacheLifetime: Infinity,
+ fetcher: (key) => this.userKeypairsRepository.findOneByOrFail({ userId: key }),
+ toRedisConverter: (value) => JSON.stringify(value),
+ fromRedisConverter: (value) => JSON.parse(value),
+ });
+ }
+
+ @bindThis
+ public async getUserKeypair(userId: User['id']): Promise<UserKeypair> {
+ return await this.cache.fetch(userId);
+ }
+}