misskey/packages/backend/src/core/UserKeypairStoreService.ts

25 lines
830 B
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import { Inject, Injectable } from '@nestjs/common';
import type { User } from '@/models/entities/User.js';
2022-09-20 20:33:11 +00:00
import type { UserKeypairsRepository } from '@/models/index.js';
2023-03-24 07:43:42 +00:00
import { KVCache } from '@/misc/cache.js';
2022-09-17 18:27:08 +00:00
import type { UserKeypair } from '@/models/entities/UserKeypair.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
2022-09-17 18:27:08 +00:00
@Injectable()
export class UserKeypairStoreService {
2023-03-24 07:43:42 +00:00
private cache: KVCache<UserKeypair>;
2022-09-17 18:27:08 +00:00
constructor(
@Inject(DI.userKeypairsRepository)
private userKeypairsRepository: UserKeypairsRepository,
) {
2023-03-24 07:43:42 +00:00
this.cache = new KVCache<UserKeypair>(Infinity);
2022-09-17 18:27:08 +00:00
}
@bindThis
2022-09-17 18:27:08 +00:00
public async getUserKeypair(userId: User['id']): Promise<UserKeypair> {
2022-09-18 18:11:50 +00:00
return await this.cache.fetch(userId, () => this.userKeypairsRepository.findOneByOrFail({ userId: userId }));
2022-09-17 18:27:08 +00:00
}
}