Every cache service in the persistence layer extendsDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-kit-redis-postgres-pgpool-docker/llms.txt
Use this file to discover all available pages before exploring further.
BaseMap(REDIS_KEY, TTL) — a factory-produced class defined in src/lib/common/BaseMap.ts. Rather than caching entire documents, each service stores only the row’s UUID string in Redis, then lets the DB service resolve the full record from Postgres in a targeted findByFilter({ id }) call that always hits the primary. TTL is set to -1 for all 16 services, meaning entries never expire and survive process restarts indefinitely. Cache consistency is maintained by the DB services, which call the appropriate set* method after every successful upsert.
The Redis value stored at each key is always a plain UUID string — never a serialized JSON document. Keeping payloads out of Redis reduces memory pressure and avoids cache-invalidation complexity when a JSONB payload is updated in Postgres.
BaseMap — base class API
All cache services inherit the following methods.connectionKey is the REDIS_KEY constant declared in each service file; it becomes the namespace prefix for every Redis key managed by that service.
clear(), iterate(), keys(), values(), and toArray() all use cursor-based SCAN with a batch size of 100. They are safe to run against large keyspaces without blocking the Redis event loop.Redis key namespacing
BaseMap._getItemKey(key) formats every Redis key as {connectionKey}:{key}. Because each service is constructed with a unique REDIS_KEY constant, there is zero risk of key collision between services sharing the same Redis instance — even services whose domain keys overlap structurally (e.g. both MeasureCacheService and IntervalCacheService use {bucket}:{entryKey}).
Service reference table
The table below lists each cache service’sREDIS_KEY prefix, the composite key formula passed to BaseMap, and the three (or four) public domain methods added by the subclass.
| Service | REDIS_KEY | Key structure | Methods |
|---|---|---|---|
CandleCacheService | candle_cache | {exchangeName}:{symbol}:{interval}:{timestamp} | hasCandleId, getCandleId, setCandleId |
SignalCacheService | signal_cache | {exchangeName}:{strategyName}:{symbol} | hasSignalId, getSignalId, setSignalId |
ScheduleCacheService | schedule_cache | {exchangeName}:{strategyName}:{symbol} | hasScheduleId, getScheduleId, setScheduleId |
StrategyCacheService | strategy_cache | {exchangeName}:{strategyName}:{symbol} | hasStrategyId, getStrategyId, setStrategyId |
RiskCacheService | risk_cache | {exchangeName}:{riskName} | hasRiskId, getRiskId, setRiskId |
PartialCacheService | partial_cache | {exchangeName}:{strategyName}:{symbol}:{signalId} | hasPartialId, getPartialId, setPartialId |
BreakevenCacheService | breakeven_cache | {exchangeName}:{strategyName}:{symbol}:{signalId} | hasBreakevenId, getBreakevenId, setBreakevenId |
StorageCacheService | storage_cache | "backtest"|"live":{signalId} | hasStorageId, getStorageId, setStorageId |
NotificationCacheService | notification_cache | "backtest"|"live":{notificationId} | hasNotificationId, getNotificationId, setNotificationId |
LogCacheService | log_cache | {entryId} | hasLogId, getLogId, setLogId |
MeasureCacheService | measure_cache | {bucket}:{entryKey} | hasMeasureId, getMeasureId, setMeasureId |
IntervalCacheService | interval_cache | {bucket}:{entryKey} | hasIntervalId, getIntervalId, setIntervalId, deleteIntervalId |
MemoryCacheService | memory_cache | {signalId}:{bucketName}:{memoryId} | hasMemoryEntryId, getMemoryEntryId, setMemoryEntryId |
RecentCacheService | recent_cache | "backtest"|"live":{exchangeName}:{strategyName}:{frameName}:{symbol} | hasRecentId, getRecentId, setRecentId |
StateCacheService | state_cache | {signalId}:{bucketName} | hasStateId, getStateId, setStateId |
SessionCacheService | session_cache | {exchangeName}:{strategyName}:{frameName}:{symbol}:"backtest"|"live" | hasSessionId, getSessionId, setSessionId |
CandleCacheService — representative method signatures
CandleCacheService is the highest-throughput service in the layer and illustrates the pattern used by all 16 services. The set* method accepts the full row (so it can extract both the composite key components and the UUID in one call) while has* and get* accept the individual key components.
_cacheKey helper — extracted directly from the source — shows exactly how Redis keys are constructed:
Cache miss behaviour
On everyfind* or has* call in a DB service, the lookup sequence is:
- Call the cache service’s
get*orhas*method. - Cache hit — use the returned UUID to call
super.findByFilter({ id })on the Postgres primary. Return the document. - Cache miss — fall back to
super.findByFilter({ naturalKey... }), which queries Postgres directly. - If Postgres returns a row, immediately call
set*to backfill the cache. Subsequent reads will hit Redis.
The cache is never the source of truth. If a Redis key is evicted, deleted, or the Redis instance is flushed, the system degrades gracefully to full Postgres queries with automatic cache rebuild on the next hit.
IntervalCacheService — extra deleteIntervalId method
IntervalCacheService is the only cache service that exposes a fourth method beyond the standard has/get/set triad. deleteIntervalId is called by IntervalDbService.clearBucket to purge cache entries for every row in a bucket before the hard DELETE is issued against Postgres, keeping the Redis namespace consistent with the database state.
Accessing cache services via the IoC container
Cache services are available on the sameioc singleton as the DB services, useful when you need to inspect or warm the cache independently of the DB layer.