Class CacheManager

Cache manager for the API client

Hierarchy

  • CacheManager

Constructors

  • Create a new cache manager

    Parameters

    • config: CacheManagerConfig = {}

      Cache manager configuration

    Returns CacheManager

Methods

  • Clear the entire cache

    Returns Promise<void>

  • Get a value from the cache

    Type Parameters

    • T

    Parameters

    • key: string

      Cache key

    Returns Promise<null | T>

    Cached value or null if not found or expired

  • Get the default TTL for cache entries

    Returns number

    Default TTL in milliseconds

  • Get the current cache strategy

    Returns CacheStrategy

    Current cache strategy

  • Invalidate a cache entry

    Parameters

    • key: string

      Cache key

    Returns Promise<void>

  • Check if the cache is enabled

    Returns boolean

    Whether the cache is enabled

  • Set a value in the cache

    Type Parameters

    • T

    Parameters

    • key: string

      Cache key

    • value: T

      Value to cache

    • Optional ttl: number

      Time to live in milliseconds (optional, defaults to defaultTtl)

    Returns Promise<void>

  • Set the default TTL for cache entries

    Parameters

    • ttl: number

      Default TTL in milliseconds

    Returns void

  • Enable or disable the cache

    Parameters

    • enabled: boolean

      Whether to enable the cache

    Returns void

  • Set the cache strategy

    Parameters

    • strategy: CacheStrategy

      Cache strategy to use

    Returns void

  • Execute a function with caching support

    Type Parameters

    • T

    Parameters

    • fn: (() => Promise<T>)

      Function to execute with caching

        • (): Promise<T>
        • Returns Promise<T>

    • Optional key: string

      Optional cache key (if not provided, caching is disabled)

    • Optional ttl: number

      Optional TTL in milliseconds

    Returns Promise<T>

    Result of the function with caching applied

  • Create a cache key from a URL and parameters

    Parameters

    • url: string

      URL

    • Optional params: Record<string, any>

      Parameters

    Returns string

    Cache key

  • Create a new cache manager with resilient caching configuration

    This factory method creates a CacheManager with a ResilientCache strategy, providing enhanced stability, error handling, and fallback capabilities.

    Features:

    • Automatic fallback to secondary cache when primary fails
    • Configurable retry attempts for transient errors
    • Automatic recovery from failures
    • Telemetry for monitoring cache health and performance
    • Timeout protection to prevent blocking operations

    Parameters

    • config: ResilientCacheConfig = {}

      Resilient cache configuration

    Returns CacheManager

    A configured CacheManager with resilient caching

    Example

    // Create a cache manager with in-memory primary and localStorage fallback
    const cacheManager = CacheManager.createResilient({
    primary: new InMemoryCache(),
    fallback: new LocalStorageCache('api_cache_'),
    retries: 3,
    telemetry: (event, details) => {
    console.log(`Cache event: ${event}`, details);
    }
    });

Generated using TypeDoc