pub struct DataCache { /* private fields */ }Expand description
Data cache with prefetched historical and live data buffers
The cache provides:
- Async prefetch of historical data (called before execution)
- Sync access to cached data (during execution)
- Live data streaming via background tasks
§Thread Safety
The live buffer uses Arc<RwLock<...>> to allow concurrent reads during
execution while background tasks write new bars.
Subscriptions use Arc<Mutex<...>> to allow shared ownership across clones.
Implementations§
Source§impl DataCache
impl DataCache
Sourcepub fn new(provider: SharedAsyncProvider, runtime: Handle) -> Self
pub fn new(provider: SharedAsyncProvider, runtime: Handle) -> Self
Create a new data cache
§Arguments
provider- Async data provider for loading dataruntime- Tokio runtime handle for spawning background tasks
Sourcepub async fn prefetch(
&self,
queries: Vec<DataQuery>,
) -> Result<(), AsyncDataError>
pub async fn prefetch( &self, queries: Vec<DataQuery>, ) -> Result<(), AsyncDataError>
Prefetch historical data for given queries (async)
This loads all queries concurrently and populates the cache. Should be called before execution starts.
§Arguments
queries- List of data queries to prefetch
§Returns
Ok if all queries loaded successfully, error otherwise.
§Example
let queries = vec![
DataQuery::new("AAPL", Timeframe::d1()).limit(1000),
DataQuery::new("MSFT", Timeframe::d1()).limit(1000),
];
cache.prefetch(queries).await?;Sourcepub fn get_row(
&self,
id: &str,
timeframe: &Timeframe,
index: usize,
) -> Option<OwnedDataRow>
pub fn get_row( &self, id: &str, timeframe: &Timeframe, index: usize, ) -> Option<OwnedDataRow>
Get row at index (sync - reads from cache)
This is the hot path - called frequently during execution. Reads are lock-free for historical data, read-locked for live data.
§Arguments
symbol- Symbol to querytimeframe- Timeframeindex- Absolute row index
§Returns
The row if available, None otherwise.
Sourcepub fn get_row_range(
&self,
id: &str,
timeframe: &Timeframe,
start: usize,
end: usize,
) -> Vec<OwnedDataRow>
pub fn get_row_range( &self, id: &str, timeframe: &Timeframe, start: usize, end: usize, ) -> Vec<OwnedDataRow>
Sourcepub fn subscribe_live(
&self,
id: &str,
timeframe: &Timeframe,
) -> Result<(), AsyncDataError>
pub fn subscribe_live( &self, id: &str, timeframe: &Timeframe, ) -> Result<(), AsyncDataError>
Start live data subscription (spawns background task)
This subscribes to live bar updates and spawns a background task that appends new bars to the live buffer as they arrive.
§Arguments
symbol- Symbol to subscribe totimeframe- Timeframe for bars
§Returns
Ok if subscription started, error otherwise. Returns Ok without action if already subscribed.
Sourcepub fn unsubscribe_live(&self, symbol: &str, timeframe: &Timeframe)
pub fn unsubscribe_live(&self, symbol: &str, timeframe: &Timeframe)
Stop live data subscription
Cancels the background task and unsubscribes from the provider.
§Arguments
symbol- Symbol to unsubscribe fromtimeframe- Timeframe
Sourcepub fn has_cached(&self, symbol: &str, timeframe: &Timeframe) -> bool
pub fn has_cached(&self, symbol: &str, timeframe: &Timeframe) -> bool
Sourcepub fn cached_keys(&self) -> Vec<(String, Timeframe)>
pub fn cached_keys(&self) -> Vec<(String, Timeframe)>
Sourcepub fn provider(&self) -> SharedAsyncProvider
pub fn provider(&self) -> SharedAsyncProvider
Get the async provider
Returns a clone of the SharedAsyncProvider for use in other components.
Sourcepub fn snapshot(&self, store: &SnapshotStore) -> AnyResult<DataCacheSnapshot>
pub fn snapshot(&self, store: &SnapshotStore) -> AnyResult<DataCacheSnapshot>
Create a snapshot of the data cache (historical + live buffers).
W17-snapshot-resume surface — see ADR-006 §2.7.4 + §2.7.5.1.
The DataFrame (de)serializers were deleted alongside the broader
nanboxed-slot snapshot helpers. The kind-threaded replacement
lands in the Phase 2c snapshot rebuild session; until then, this
method returns a structured anyhow! error rather than panicking
via todo!() (the strict improvement over a todo!()-driven
process abort).
Sourcepub fn restore_from_snapshot(
&self,
_snapshot: DataCacheSnapshot,
_store: &SnapshotStore,
) -> AnyResult<()>
pub fn restore_from_snapshot( &self, _snapshot: DataCacheSnapshot, _store: &SnapshotStore, ) -> AnyResult<()>
Restore data cache contents from a snapshot.
See Self::snapshot — W17-snapshot-resume surface.