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).
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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for DataCache
impl !RefUnwindSafe for DataCache
impl Send for DataCache
impl Sync for DataCache
impl Unpin for DataCache
impl UnsafeUnpin for DataCache
impl !UnwindSafe for DataCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more