Files
lolympus-tournament-support/apps/api/src/modules/global/redis/redis.service.ts
T

294 lines
7.6 KiB
TypeScript

import { Inject, Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
import type { createClient } from 'redis';
import { REDIS_CLIENT } from './redis.constants';
export type RedisClient = ReturnType<typeof createClient>;
export interface StreamEntry {
deliveriesCounter?: number;
id: string;
message: Record<string, string>;
millisElapsedFromDelivery?: number;
}
export interface StreamReadResult {
messages: StreamEntry[];
name: string;
}
export interface ClaimStalePendingStreamMessagesResult {
messages: StreamEntry[];
nextStartId: string;
}
interface RawStreamMessage {
deliveriesCounter?: number;
id: string;
message: unknown;
millisElapsedFromDelivery?: number;
}
interface RawStreamReadResult {
messages: RawStreamMessage[];
name: string;
}
function normalizeStreamFields(fields: unknown): Record<string, string> {
if (Array.isArray(fields)) {
const normalized: Record<string, string> = {};
for (let index = 0; index < fields.length; index += 2) {
const key = fields[index];
if (key === undefined) continue;
normalized[String(key)] = String(fields[index + 1] ?? '');
}
return normalized;
}
if (fields instanceof Map) {
return Object.fromEntries(
[...fields.entries()].map(([key, value]) => [String(key), String(value)]),
);
}
if (typeof fields === 'object' && fields !== null) {
return Object.fromEntries(
Object.entries(fields as Record<string, unknown>).map(([key, value]) => [
key,
String(value),
]),
);
}
return {};
}
@Injectable()
export class RedisService implements OnModuleDestroy {
private readonly logger = new Logger(RedisService.name);
constructor(@Inject(REDIS_CLIENT) private readonly client: RedisClient) {}
async onModuleDestroy() {
try {
await this.client.quit();
} catch {
this.client.destroy();
}
}
public raw(): RedisClient {
return this.client;
}
public async getJson<T>(key: string): Promise<T | null> {
const raw = await this.client.get(key);
if (!raw) return null;
try {
return JSON.parse(raw) as T;
} catch (err) {
this.logger.warn(
`Failed to JSON.parse cached value for key="${key}": ${
(err as Error)?.message ?? err
}`,
);
return null;
}
}
public async setJson(
key: string,
value: unknown,
ttlSeconds?: number,
): Promise<void> {
const serialized = JSON.stringify(value);
if (ttlSeconds && ttlSeconds > 0) {
await this.client.set(key, serialized, { EX: ttlSeconds });
return;
}
await this.client.set(key, serialized);
}
public async getString(key: string): Promise<string | null> {
return this.client.get(key);
}
public async setString(
key: string,
value: string,
ttlSeconds?: number,
): Promise<void> {
if (ttlSeconds && ttlSeconds > 0) {
await this.client.set(key, value, { EX: ttlSeconds });
return;
}
await this.client.set(key, value);
}
public async setStringIfAbsent(
key: string,
value: string,
ttlSeconds?: number,
): Promise<boolean> {
const options: { EX?: number; NX: true } = { NX: true };
if (ttlSeconds && ttlSeconds > 0) options.EX = ttlSeconds;
const result = await this.client.set(key, value, options);
return result === 'OK';
}
public async delete(key: string): Promise<number> {
return this.client.del(key);
}
public async appendStream(
key: string,
fields: Record<string, string>,
options?: { maxLen?: number },
): Promise<string> {
const trimOptions =
options?.maxLen && options.maxLen > 0
? {
TRIM: {
strategy: 'MAXLEN' as const,
strategyModifier: '~' as const,
threshold: options.maxLen,
},
}
: undefined;
return this.client.xAdd(key, '*', fields, trimOptions);
}
public async createConsumerGroup(
key: string,
group: string,
startId = '$',
): Promise<void> {
await this.client.xGroupCreate(key, group, startId, { MKSTREAM: true });
}
public async readGroup(
key: string,
group: string,
consumer: string,
startId: string,
options?: { blockMs?: number; count?: number },
): Promise<StreamReadResult[] | null> {
const readOptions: { BLOCK?: number; COUNT?: number } = {};
if (options?.blockMs !== undefined) readOptions.BLOCK = options.blockMs;
if (options?.count !== undefined) readOptions.COUNT = options.count;
const reply = await this.client.xReadGroup(
group,
consumer,
{ key, id: startId },
readOptions,
);
if (!reply) return null;
return (reply as RawStreamReadResult[]).map((stream) => ({
name: String(stream.name),
messages: stream.messages.map((message) => ({
deliveriesCounter:
typeof message.deliveriesCounter === 'number'
? message.deliveriesCounter
: undefined,
id: String(message.id),
message: normalizeStreamFields(message.message),
millisElapsedFromDelivery:
typeof message.millisElapsedFromDelivery === 'number'
? message.millisElapsedFromDelivery
: undefined,
})),
}));
}
public async claimStalePendingStreamMessages(options: {
key: string;
group: string;
consumer: string;
stalePendingMessageMinimumIdleMilliseconds: number;
startId?: string;
count?: number;
}): Promise<ClaimStalePendingStreamMessagesResult> {
const response = (await this.client.sendCommand([
'XAUTOCLAIM',
options.key,
options.group,
options.consumer,
String(options.stalePendingMessageMinimumIdleMilliseconds),
options.startId ?? '0-0',
'COUNT',
String(options.count ?? 1),
])) as unknown;
const responseItems = Array.isArray(response) ? response : [];
const rawMessages = Array.isArray(responseItems[1]) ? responseItems[1] : [];
return {
nextStartId: String(responseItems[0] ?? '0-0'),
messages: rawMessages
.map((rawMessage) => this.normalizeClaimedStreamMessage(rawMessage))
.filter((message): message is StreamEntry => Boolean(message)),
};
}
public async acknowledgeStream(
key: string,
group: string,
ids: string | string[],
): Promise<number> {
return this.client.xAck(key, group, ids);
}
public async deleteStreamEntries(
key: string,
ids: string | string[],
): Promise<number> {
return this.client.xDel(key, ids);
}
public async streamLength(key: string): Promise<number> {
return this.client.xLen(key);
}
public async publish(channel: string, message: string): Promise<number> {
return this.client.publish(channel, message);
}
private normalizeClaimedStreamMessage(
rawMessage: unknown,
): StreamEntry | null {
if (Array.isArray(rawMessage)) {
const [id, fields] = rawMessage;
if (id === undefined) return null;
return {
id: String(id),
message: normalizeStreamFields(fields),
};
}
if (
typeof rawMessage === 'object' &&
rawMessage !== null &&
'id' in rawMessage
) {
const message = rawMessage as RawStreamMessage;
return {
deliveriesCounter:
typeof message.deliveriesCounter === 'number'
? message.deliveriesCounter
: undefined,
id: String(message.id),
message: normalizeStreamFields(message.message),
millisElapsedFromDelivery:
typeof message.millisElapsedFromDelivery === 'number'
? message.millisElapsedFromDelivery
: undefined,
};
}
return null;
}
}