Skip to main content
The makeCacheableSignalKeyStore function wraps a SignalKeyStore with an in-memory cache to reduce database queries and improve performance.

Function Signature

Parameters

SignalKeyStore
required
The underlying key store to add caching to. Can be any implementation of SignalKeyStore.
ILogger
Optional logger to trace cache operations. Logs events like:
  • Cache hits/misses
  • Number of items loaded from store
  • Cache update operations
CacheStore
Optional custom cache implementation. If not provided, uses a default NodeCache with:
  • TTL: 5 minutes (300 seconds)
  • useClones: false (stores references, not copies)
  • deleteOnExpire: true (automatically removes expired entries)

Returns

SignalKeyStore
required
A cached wrapper around the original store with the same interface

Usage Example

How It Works

Cache Keys

Cache keys are generated using a combination of type and ID:

Get Operation Flow

  1. Check cache for each requested ID
  2. Collect cache misses in idsToFetch array
  3. Query store only for missing IDs
  4. Update cache with fetched items
  5. Return combined results (cached + fetched)

Set Operation Flow

  1. Update cache with all provided data
  2. Write to store (pass-through to underlying implementation)
  3. Log operation if logger provided

Thread Safety

All cache operations are protected by a mutex to prevent race conditions:

Performance Benefits

Frequently accessed keys (like sessions) are served from memory, significantly reducing database load.
In-memory cache access is orders of magnitude faster than disk/database access.
Default 5-minute TTL ensures cache doesn’t grow unbounded while keeping hot data available.

Custom Cache Implementation

You can provide a custom cache that implements the CacheStore interface:

Example: Redis Cache