LRU (Least Recently Used) cache implementation for query results with TTL support. This cache provides efficient storage and retrieval of query results with automatic expiration based on time-to-live settings and size limits.

Constructors

Properties

DEFAULT_MAX_SIZE: 1000 = 1000
DEFAULT_TTL_MINUTES: 60 = 60
accessOrder: string[] = []
cache: Map<string, QueryCacheEntry> = ...
stats: {
    evictions: number;
    expirations: number;
    hits: number;
    misses: number;
} = ...

Performance statistics for cache monitoring

Type declaration

  • evictions: number
  • expirations: number
  • hits: number
  • misses: number

Methods

  • Clean up expired entries from the cache. This can be called periodically to free up memory.

    Returns number

    Number of expired entries removed

  • Clear cache for specific query or all queries.

    Parameters

    • Optional queryId: string

      Optional query ID to clear specific query cache

    Returns void

  • Get cached results if available and not expired. Updates access order for LRU tracking and increments hit counter.

    Parameters

    • queryId: string

      The query identifier

    • params: Record<string, any>

      The query parameters

    • config: QueryCacheConfig

      Cache configuration settings

    Returns QueryCacheEntry

    The cached entry if valid, null otherwise

  • Generate a deterministic cache key from query ID and parameters. The key is created by sorting parameter keys and creating a stable JSON representation.

    Parameters

    • queryId: string

      The unique query identifier

    • params: Record<string, any>

      The query parameters

    Returns string

    A stable cache key string

  • Get cache statistics for monitoring and debugging.

    Returns {
        evictions: number;
        expirations: number;
        hitRate: number;
        hits: number;
        misses: number;
        size: number;
    }

    Object containing cache performance metrics

    • evictions: number
    • expirations: number
    • hitRate: number
    • hits: number
    • misses: number
    • size: number
  • Remove a key from the access order tracking.

    Parameters

    • key: string

      The cache key to remove

    Returns void

  • Cache query results with TTL and LRU eviction. Evicts least recently used entries when at capacity.

    Parameters

    • queryId: string

      The query identifier

    • params: Record<string, any>

      The query parameters

    • results: any[]

      The query results to cache

    • config: QueryCacheConfig

      Cache configuration settings

    Returns void

  • Update the access order for LRU tracking. Moves the accessed key to the end of the array (most recently used).

    Parameters

    • key: string

      The cache key that was accessed

    Returns void