2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-12 03:37:45 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-10 06:45:11 +02:00
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2023-04-14 06:50:05 +02:00
|
|
|
import * as Redis from 'ioredis';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { UserKeypairsRepository } from '@/models/_.js';
|
2023-04-05 05:10:40 +02:00
|
|
|
import { RedisKVCache } from '@/misc/cache.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUserKeypair } from '@/models/UserKeypair.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
2023-06-10 06:45:11 +02:00
|
|
|
export class UserKeypairService implements OnApplicationShutdown {
|
2023-08-16 10:51:28 +02:00
|
|
|
private cache: RedisKVCache<MiUserKeypair>;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
constructor(
|
2023-04-05 05:10:40 +02:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.userKeypairsRepository)
|
|
|
|
private userKeypairsRepository: UserKeypairsRepository,
|
|
|
|
) {
|
2023-08-16 10:51:28 +02:00
|
|
|
this.cache = new RedisKVCache<MiUserKeypair>(this.redisClient, 'userKeypair', {
|
2023-04-05 05:10:40 +02:00
|
|
|
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 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async getUserKeypair(userId: MiUser['id']): Promise<MiUserKeypair> {
|
2023-04-05 05:10:40 +02:00
|
|
|
return await this.cache.fetch(userId);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2023-06-10 06:45:11 +02:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public dispose(): void {
|
|
|
|
this.cache.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|