Skip to main content

DataCache

Struct DataCache 

Source
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

Source

pub fn new(provider: SharedAsyncProvider, runtime: Handle) -> Self

Create a new data cache

§Arguments
  • provider - Async data provider for loading data
  • runtime - Tokio runtime handle for spawning background tasks
Source

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?;
Source

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 query
  • timeframe - Timeframe
  • index - Absolute row index
§Returns

The row if available, None otherwise.

Source

pub fn get_row_range( &self, id: &str, timeframe: &Timeframe, start: usize, end: usize, ) -> Vec<OwnedDataRow>

Get row range (sync - reads from cache)

§Arguments
  • symbol - Symbol to query
  • timeframe - Timeframe
  • start - Start index (inclusive)
  • end - End index (exclusive)
§Returns

Vector of rows in the range. May be shorter than requested if data unavailable.

Source

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 to
  • timeframe - Timeframe for bars
§Returns

Ok if subscription started, error otherwise. Returns Ok without action if already subscribed.

Source

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 from
  • timeframe - Timeframe
Source

pub fn row_count(&self, id: &str, timeframe: &Timeframe) -> usize

Get total row count (historical + live)

§Arguments
  • symbol - Symbol to query
  • timeframe - Timeframe
§Returns

Total number of rows available (historical + live).

Source

pub fn has_cached(&self, symbol: &str, timeframe: &Timeframe) -> bool

Check if data is cached

§Arguments
  • symbol - Symbol to check
  • timeframe - Timeframe to check
§Returns

true if historical data is cached for this key.

Source

pub fn cached_keys(&self) -> Vec<(String, Timeframe)>

Get list of cached symbols

§Returns

Vector of (symbol, timeframe) pairs that are cached.

Source

pub fn clear(&self)

Clear all cached data

Stops all subscriptions and clears all cached data.

Source

pub fn provider(&self) -> SharedAsyncProvider

Get the async provider

Returns a clone of the SharedAsyncProvider for use in other components.

Source

pub fn snapshot(&self, store: &SnapshotStore) -> AnyResult<DataCacheSnapshot>

Create a snapshot of the data cache (historical + live buffers).

Source

pub fn restore_from_snapshot( &self, snapshot: DataCacheSnapshot, store: &SnapshotStore, ) -> AnyResult<()>

Restore data cache contents from a snapshot.

Trait Implementations§

Source§

impl Clone for DataCache

Source§

fn clone(&self) -> DataCache

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for DataCache

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more