All files / src/cache cache.ts

89.28% Statements 75/84
75% Branches 9/12
71.42% Functions 5/7
89.28% Lines 75/84

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 851x 1x 1x 1x 1x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 67x 67x     67x 67x 103x 103x 103x 103x 103x 103x 103x 103x 65x 6x 6x 59x 62x       59x 59x 59x 59x 103x 103x 103x 103x 103x 103x     103x 103x 103x 103x 103x 103x 103x     103x  
import { totalmem } from 'os'
 
/**
 * Represents an in-memory cache with TTL (Time To Live) support.
 */
export class Cache {
  private cache = new Map<
    string,
    {
      ttl: number
      value: any
    }
  >()
 
  private maxCacheCap: number
 
  /**
   * Creates an instance of Cache.
   */
  constructor() {
    this.maxCacheCap = this.calculateMaxCapacity()
  }
 
  /**
   * Calculates the maximum capacity of the cache based on available system memory.
   * @returns The maximum capacity of the cache.
   */
  private calculateMaxCapacity(): number {
    const totalMemory = totalmem()
    const maxCapacityPercentage = 0.2
    const maxCapacityBytes = totalMemory * maxCapacityPercentage
    return Math.floor(maxCapacityBytes / (1024 * 50))
  }
 
  /**
   * Retrieves the value associated with the specified key from the cache.
   * @param key - The key to look up in the cache.
   * @returns The cached value if found and not expired; otherwise, returns null.
   */
  get(key: string): any | null {
    const entry = this.cache.get(key)
    if (entry && Date.now() < entry.ttl) {
      return entry.value
    }
    return null
  }
 
  /**
   * Stores a key-value pair in the cache with a specified TTL (Time To Live).
   * @param key - The key to store in the cache.
   * @param value - The value to associate with the key.
   * @param ttl - The TTL (Time To Live) in seconds for the cached entry.
   */
  set(key: string, value: any, ttl: number): void {
    if (ttl < 1) {
      return
    }
 
    if (this.cache.size >= this.maxCacheCap) {
      const oldestKey = this.cache.keys().next().value
      this.cache.delete(oldestKey)
    }
    const expireTime = Date.now() + ttl * 1000
    const entry = { value, ttl: expireTime }
    this.cache.set(key, entry)
  }
 
  /**
   * Removes the entry with the specified key from the cache.
   * @param key - The key to delete from the cache.
   */
  delete(key: string): void {
    this.cache.delete(key)
  }
 
  /**
   * Checks if the cache contains an entry with the specified key.
   * @param key - The key to check in the cache.
   * @returns True if the cache contains the key; otherwise, false.
   */
  has(key: string): boolean {
    return this.get(key) !== null
  }
}