2022-09-17 13:27:08 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-13 23:50:05 -05:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 13:27:08 -05:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
2022-09-20 15:33:11 -05:00
|
|
|
import type { UserKeypairsRepository } from '@/models/index.js';
|
2023-04-04 22:10:40 -05:00
|
|
|
import { RedisKVCache } from '@/misc/cache.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import type { UserKeypair } from '@/models/entities/UserKeypair.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Injectable()
|
2023-04-04 22:10:40 -05:00
|
|
|
export class UserKeypairService {
|
|
|
|
private cache: RedisKVCache<UserKeypair>;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
constructor(
|
2023-04-04 22:10:40 -05:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
@Inject(DI.userKeypairsRepository)
|
|
|
|
private userKeypairsRepository: UserKeypairsRepository,
|
|
|
|
) {
|
2023-04-04 22:10:40 -05:00
|
|
|
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),
|
|
|
|
});
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public async getUserKeypair(userId: User['id']): Promise<UserKeypair> {
|
2023-04-04 22:10:40 -05:00
|
|
|
return await this.cache.fetch(userId);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
}
|