All files / src/common/services cache.service.ts

0% Statements 0/55
0% Branches 0/19
0% Functions 0/5
0% Lines 0/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                                                                                                                                                                                         
import { Injectable, Inject, OnModuleDestroy } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import Redlock from 'redlock';
 
@Injectable()
export class CacheService implements OnModuleDestroy {
  private redlock!: any;
  // Placeholder counters for Prometheus metrics
  public cacheHits = 0;
  public cacheMisses = 0;
  public lockFailures = 0;
  public redisErrors = 0;
 
  constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {
    const store = (this.cacheManager as any).store;
    const redisClient = store.getClient();
    this.redlock = new Redlock([redisClient], {/*...*/});
  }
 
  // Write-through: update cache after DB write
  async set<T>(key: string, value: T, ttl = 60): Promise<void> {
    try {
      await this.cacheManager.set(key, value, ttl);
    } catch (err) {
      this.redisErrors++;
      // TODO: enqueue to outbox for retry
      console.error('Redis set error, fallback to outbox:', err);
    }
  }
 
  // Lazy: get from cache, or DB fallback with thundering herd protection
  async getOrSet<T>(key: string, fetcher: () => Promise<T>, ttl = 60): Promise<T> {
    try {
      let value = await this.cacheManager.get<T>(key);
      Iif (value !== undefined && value !== null) {
        this.cacheHits++;
        return value;
      }
      this.cacheMisses++;
      // Use Redlock to prevent thundering herd
      let lock;
      try {
        lock = await this.redlock.acquire([`lock:${key}`], 1000);
      } catch (err) {
        this.lockFailures++;
        // If lock fails, fallback to direct DB fetch
        console.warn('Redlock acquire failed, fallback to DB:', err);
        return fetcher();
      }
      try {
        // Double check after acquiring lock
        value = await this.cacheManager.get<T>(key);
        Iif (value !== undefined && value !== null) {
          this.cacheHits++;
          return value;
        }
        value = await fetcher();
        await this.cacheManager.set(key, value, ttl);
        return value;
      } finally {
        await lock.release();
      }
    } catch (err) {
      this.redisErrors++;
      // On any Redis error, fallback to DB
      console.error('Redis getOrSet error, fallback to DB:', err);
      return fetcher();
    }
  }
 
  async del(key: string): Promise<void> {
    try {
      await this.cacheManager.del(key);
    } catch (err) {
      this.redisErrors++;
      // TODO: enqueue to outbox for retry
      console.error('Redis del error, fallback to outbox:', err);
    }
  }
 
  async onModuleDestroy() {
    try {
      // Close Redlock connections
      Iif (this.redlock) {
        await this.redlock.quit();
      }
      
      // Close Redis connections from cache manager
      const store = (this.cacheManager as any).store;
      Iif (store && store.getClient) {
        const redisClient = store.getClient();
        Iif (redisClient && redisClient.quit) {
          await redisClient.quit();
        }
      }
    } catch (error) {
      console.error('Error closing cache connections:', error);
    }
  }
}