import { Inject, Injectable } from "@nestjs/common"; import Redis from "ioredis"; import { Logger } from "nestjs-pino"; @Injectable() export class CacheService { constructor( @Inject("REDIS_CLIENT") private readonly redis: Redis, @Inject(Logger) private readonly logger: Logger ) {} async get(key: string): Promise { const value = await this.redis.get(key); if (!value) { return null; } try { return JSON.parse(value) as T; } catch (error) { this.logger.warn({ key, error }, "Failed to parse cached value; evicting entry"); await this.redis.del(key); return null; } } async set(key: string, value: unknown, ttlSeconds?: number): Promise { const serialized = JSON.stringify(value); if (ttlSeconds !== undefined) { const ttl = Math.max(0, Math.floor(ttlSeconds)); if (ttl > 0) { await this.redis.set(key, serialized, "EX", ttl); } else { // Allow callers to request immediate expiry without leaking stale values await this.redis.set(key, serialized, "PX", 1); } return; } await this.redis.set(key, serialized); } async del(key: string): Promise { await this.redis.del(key); } async delPattern(pattern: string): Promise { const pipeline = this.redis.pipeline(); let pending = 0; const flush = async () => { if (pending === 0) { return; } await pipeline.exec(); pending = 0; }; await this.scanPattern(pattern, async keys => { keys.forEach(key => { pipeline.del(key); pending += 1; }); if (pending >= 1000) { await flush(); } }); await flush(); } async countByPattern(pattern: string): Promise { let total = 0; await this.scanPattern(pattern, keys => { total += keys.length; }); return total; } async memoryUsageByPattern(pattern: string): Promise { let total = 0; await this.scanPattern(pattern, async keys => { const pipeline = this.redis.pipeline(); keys.forEach(key => { pipeline.memory("usage", key); }); const results = await pipeline.exec(); if (!results) { return; } results.forEach(result => { if (!result) { return; } const [error, usage] = result as [Error | null, unknown]; if (!error && typeof usage === "number") { total += usage; } }); }); return total; } async exists(key: string): Promise { return (await this.redis.exists(key)) === 1; } buildKey(prefix: string, userId: string, ...parts: string[]): string { return [prefix, userId, ...parts].join(":"); } async getOrSet(key: string, fetcher: () => Promise, ttlSeconds: number = 300): Promise { const cached = await this.get(key); if (cached !== null) { return cached; } const fresh = await fetcher(); await this.set(key, fresh, ttlSeconds); return fresh; } private async scanPattern( pattern: string, onKeys: (keys: string[]) => Promise | void ): Promise { let cursor = "0"; do { const [next, keys] = (await this.redis.scan( cursor, "MATCH", pattern, "COUNT", 1000 )) as unknown as [string, string[]]; cursor = next; if (keys && keys.length) { await onKeys(keys); } } while (cursor !== "0"); } }