import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { setTimeout as delay } from 'node:timers/promises'; import type { AppConfig } from '../../config/app-config.schema.js'; import { RedisClient } from '../../redis/redis.client.js'; import type { Page } from '../../common/http/page.js'; import { MetricsService } from '../../metrics/metrics.service.js'; const LOCK_RETRY_COUNT = 4; const LOCK_RETRY_DELAY_MS = 25; export type CachePage = Page; @Injectable() export class MessagesCacheService { private readonly logger = new Logger(MessagesCacheService.name); constructor( private readonly redis: RedisClient, private readonly config: ConfigService, private readonly metrics: MetricsService, ) {} async getOrLoad( chatId: string, version: bigint, cursor: string | undefined, limit: number, loader: () => Promise, ): Promise { const key = this.cacheKey(chatId, version, cursor, limit); if (this.config.getOrThrow('CACHE_MODE') === 'naive') { return this.loadNaively(key, loader); } let cached: string | null; try { cached = await this.redis.get(key); } catch (error) { return this.fallbackToDatabase('read', error, loader); } const cachedPage = this.parsePage(cached); if (cachedPage) { this.metrics.increment('cache_hit'); return cachedPage; } this.metrics.increment('cache_miss'); const lockKey = `lock:${key}`; const owner = `${process.pid}:${Math.random().toString(36).slice(2)}`; for (let attempt = 0; attempt < LOCK_RETRY_COUNT; attempt += 1) { let locked: boolean; try { locked = await this.redis.setNxPx( lockKey, owner, this.config.getOrThrow('CACHE_LOCK_TTL_MS'), ); } catch (error) { return this.fallbackToDatabase('lock', error, loader); } if (locked) return this.fillLocked(key, lockKey, owner, loader); if (attempt === 0) this.metrics.increment('cache_lock_wait'); try { const waitedPage = this.parsePage(await this.redis.get(key)); if (waitedPage) { this.metrics.increment('cache_hit'); return waitedPage; } } catch (error) { return this.fallbackToDatabase('waiting read', error, loader); } await delay(LOCK_RETRY_DELAY_MS); } // The owner did not publish the page within the bounded wait: serve the // authoritative result instead of blocking the request further. this.metrics.increment('cache_lock_timeout'); return loader(); } private async fillLocked( key: string, lockKey: string, owner: string, loader: () => Promise, ): Promise { try { let secondCheck: string | null; try { secondCheck = await this.redis.get(key); } catch (error) { return this.fallbackToDatabase('double-check', error, loader); } const secondPage = this.parsePage(secondCheck); if (secondPage) { this.metrics.increment('cache_hit'); return secondPage; } const page = await loader(); try { await this.redis.set(key, JSON.stringify(page), { EX: this.config.getOrThrow('CACHE_TTL_SECONDS'), }); } catch (error) { // Redis is derived state: the page is still valid for this response, // but the failure must stay visible in metrics and logs. this.metrics.increment('cache_write_error'); this.logger.warn( `Cache fill failed: ${error instanceof Error ? error.message : 'unknown error'}`, ); } return page; } finally { try { await this.redis.unlock(lockKey, owner); } catch (error) { // The lock has a TTL, so a stale lock expires on its own; a successful // response must not be replaced by an unlock failure. this.metrics.increment('cache_unlock_error'); this.logger.warn( `Cache unlock failed: ${error instanceof Error ? error.message : 'unknown error'}`, ); } } } private cacheKey( chatId: string, version: bigint, cursor: string | undefined, limit: number, ): string { return `messages:${chatId}:v${version.toString()}:c${cursor ?? 'start'}:l${limit}`; } private async loadNaively( key: string, loader: () => Promise, ): Promise { let cached: string | null; try { cached = await this.redis.get(key); } catch (error) { return this.fallbackToDatabase('naive read', error, loader); } const cachedPage = this.parsePage(cached); if (cachedPage) { this.metrics.increment('cache_hit'); return cachedPage; } this.metrics.increment('cache_miss'); const cacheMissDelay = this.config.getOrThrow('CACHE_MISS_DELAY_MS'); if (cacheMissDelay > 0) await delay(cacheMissDelay); const page = await loader(); try { await this.redis.set(key, JSON.stringify(page), { EX: this.config.getOrThrow('CACHE_TTL_SECONDS'), }); } catch (error) { this.metrics.increment('cache_write_error'); this.logger.warn( `Cache fill failed: ${error instanceof Error ? error.message : 'unknown error'}`, ); } return page; } private async fallbackToDatabase( stage: string, error: unknown, loader: () => Promise, ): Promise { this.metrics.increment('cache_fallback'); this.logger.warn( `Cache ${stage} failed, serving PostgreSQL: ${error instanceof Error ? error.message : 'unknown error'}`, ); return loader(); } private parsePage(value: string | null): CachePage | undefined { if (!value) return undefined; try { const parsed: unknown = JSON.parse(value); if ( typeof parsed !== 'object' || parsed === null || !('items' in parsed) || !('next_cursor' in parsed) || !Array.isArray(parsed.items) || (parsed.next_cursor !== null && typeof parsed.next_cursor !== 'string') ) return undefined; return { items: parsed.items, next_cursor: parsed.next_cursor }; } catch { // A malformed entry is treated as a miss and overwritten by the owner fill. this.metrics.increment('cache_corrupt_entry'); return undefined; } } }