Utility functions for common cache operations and key generation.

Constructors

Methods

  • Retrieves multiple values from the cache in parallel.

    Type Parameters

    • T

      The type of cached values

    Parameters

    Returns Promise<Map<string, null | T>>

    Map of keys to their cached values (or null if not found)

    const results = await CacheUtils.batchGet(cache, ['key1', 'key2', 'key3']);
    console.log(results.get('key1')); // cached value or null
  • Stores multiple key-value pairs in the cache in parallel.

    Type Parameters

    • T

      The type of values to cache

    Parameters

    Returns Promise<void>

    await CacheUtils.batchSet(cache, [
    { key: 'key1', value: 'value1', options: { ttl: 5000 } },
    { key: 'key2', value: 'value2' }
    ]);
  • Creates a memoizer function bound to a specific cache instance. Useful for creating multiple memoized functions with the same cache and default options.

    Parameters

    Returns (<F>(fn: F, options?: node.common.apis.cache.MemoizeOptions) => F)

    A memoizer function

    const cache = new MemoryCache();
    const memoizer = CacheUtils.createMemoizer(cache, {
    ttl: 300000,
    namespace: 'api'
    });

    const memoizedFetch = memoizer(fetchUserData);
    const memoizedCalc = memoizer(expensiveCalculation, { ttl: 600000 });
  • Generates a hash-based cache key from input data. Uses SHA256 for collision resistance and security.

    Parameters

    • input: string | object

      String or object to generate a key from

    Returns Promise<string>

    A promise that resolves to a SHA256 hash string suitable for use as a cache key

    const key1 = await CacheUtils.generateKey('some text content');
    const key2 = await CacheUtils.generateKey({ prompt: 'extract', content: 'medical text' });
  • Synchronous key generation for sync functions

    Parameters

    • args: any[]

    Returns string

  • Check if a function is async

    Parameters

    • fn: Function

    Returns boolean

  • Memoizes a function using the provided cache instance. Creates a cached version of the function that stores results and returns cached values for repeated calls.

    Type Parameters

    • T extends ((...args: any[]) => any)

      The function type to memoize

    Parameters

    Returns T

    A memoized version of the function

    const cache = new MemoryCache();
    const memoizedFn = CacheUtils.memoize(expensiveFunction, cache, {
    ttl: 300000, // 5 minutes
    namespace: 'calculations',
    keyGenerator: (args) => `custom:${args[0]}`
    });

    // First call - executes function and caches result
    const result1 = await memoizedFn(123);
    // Second call - returns cached result
    const result2 = await memoizedFn(123);
  • Improved memoize function that properly handles both sync and async functions. For async functions, it caches the resolved value, not the promise itself. For sync functions, it caches the return value directly.

    Type Parameters

    • T extends ((...args: any[]) => any)

      The function type to memoize

    Parameters

    Returns T

    A memoized version of the function