Skip to content

@livefolio/sdk / FeatureCache

Interface: FeatureCache

Defined in: interfaces/feature-cache.ts:87

Content-addressed cache for feature computation results.

Implementations MUST guarantee:

  • get returns the exact Series previously stored under key, or undefined if no entry exists. It MUST NOT return a stale or partially overlapping series.
  • set stores series under key and makes it immediately available to subsequent get calls.
  • invalidate (optional) removes all entries whose key fields match the supplied prefix. Partial matches (specifying only feature, for example) MUST invalidate every key that shares those fields, regardless of the remaining fields.
  • All methods are async; implementations backed by in-process Maps may resolve synchronously via Promise.resolve.

Reference implementation: MemoryFeatureCache — in-process Map, no eviction, suitable for single-run backtests.

Example

ts
import { MemoryFeatureCache } from '@livefolio/sdk';
import type { FeatureKey, Series } from '@livefolio/sdk';

const cache = new MemoryFeatureCache();
const key: FeatureKey = {
  feature:    'sma',
  paramsHash: 'abc123',
  scope:      { kind: 'asset', asset: 'AAPL' },
  range:      { from: new Date('2024-01-01'), to: new Date('2025-01-01') },
  freq:       '1d',
};
const series: Series = [{ t: new Date('2024-01-02'), v: 150.5 }];

await cache.set(key, series);
const hit = await cache.get(key); // same reference

Methods

get()

get(key): Promise<Series | undefined>

Defined in: interfaces/feature-cache.ts:95

Retrieves the cached Series for key, or undefined on a cache miss.

Parameters

ParameterTypeDescription
keyFeatureKeyFully-qualified cache key.

Returns

Promise<Series | undefined>

The stored series, or undefined if not present.


invalidate()?

optional invalidate(prefix): Promise<void>

Defined in: interfaces/feature-cache.ts:115

Removes all cache entries whose keys match the supplied prefix. Optional — implementations that do not support invalidation may omit this.

Matching is field-by-field: only the fields present in prefix are compared; all others are treated as wildcards.

Parameters

ParameterTypeDescription
prefixPartial<FeatureKey>Partial FeatureKey. Omitted fields match any value.

Returns

Promise<void>


set()

set(key, series): Promise<void>

Defined in: interfaces/feature-cache.ts:104

Stores series under key. Overwrites any existing entry at that key.

Parameters

ParameterTypeDescription
keyFeatureKeyFully-qualified cache key.
seriesSeriesThe computed series to store. Must not be mutated after passing to set.

Returns

Promise<void>

Released under the MIT License.