pub struct MarketCache { /* private fields */ }Expand description
Market data cache structure with fine-grained locking.
Uses DashMap instead of RwLock<HashMap> to reduce lock contention
in high-concurrency scenarios. Each key-value pair has its own lock,
allowing concurrent reads and writes to different keys.
§Performance Characteristics
- Concurrent reads: O(1) with no blocking
- Concurrent writes to different keys: O(1) with no blocking
- Writes to the same key: Serialized per-key
§Example
use ccxt_core::base_exchange::MarketCache;
let cache = MarketCache::default();
// Multiple threads can read different markets concurrently
// without blocking each otherImplementations§
Source§impl MarketCache
impl MarketCache
Sourcepub fn with_capacity(
markets_capacity: usize,
currencies_capacity: usize,
) -> Self
pub fn with_capacity( markets_capacity: usize, currencies_capacity: usize, ) -> Self
Creates a new market cache with pre-allocated capacity.
Use this when you know the approximate number of markets to avoid reallocations during loading.
Sourcepub fn get_market(&self, symbol: &str) -> Option<Arc<Market>>
pub fn get_market(&self, symbol: &str) -> Option<Arc<Market>>
Gets a market by symbol.
This is a lock-free read operation.
Sourcepub fn get_market_by_id(&self, id: &str) -> Option<Arc<Market>>
pub fn get_market_by_id(&self, id: &str) -> Option<Arc<Market>>
Gets a market by exchange-specific ID.
This is a lock-free read operation.
Sourcepub fn get_currency(&self, code: &str) -> Option<Arc<Currency>>
pub fn get_currency(&self, code: &str) -> Option<Arc<Currency>>
Gets a currency by code.
This is a lock-free read operation.
Sourcepub fn get_currency_by_id(&self, id: &str) -> Option<Arc<Currency>>
pub fn get_currency_by_id(&self, id: &str) -> Option<Arc<Currency>>
Gets a currency by exchange-specific ID.
This is a lock-free read operation.
Sourcepub fn market_count(&self) -> usize
pub fn market_count(&self) -> usize
Returns the number of cached markets.
Sourcepub fn currency_count(&self) -> usize
pub fn currency_count(&self) -> usize
Returns the number of cached currencies.
Sourcepub fn markets(&self) -> Arc<HashMap<String, Arc<Market>>>
pub fn markets(&self) -> Arc<HashMap<String, Arc<Market>>>
Returns all markets as a new HashMap.
Note: This creates a new HashMap and clones all Arc references.
For iteration, consider using iter_markets() instead.
Sourcepub fn iter_markets(&self) -> impl Iterator<Item = (String, Arc<Market>)> + '_
pub fn iter_markets(&self) -> impl Iterator<Item = (String, Arc<Market>)> + '_
Iterates over all markets.
This is more efficient than markets() when you only need to iterate.
Sourcepub fn contains_market(&self, symbol: &str) -> bool
pub fn contains_market(&self, symbol: &str) -> bool
Checks if a market exists by symbol.
Sourcepub fn contains_currency(&self, code: &str) -> bool
pub fn contains_currency(&self, code: &str) -> bool
Checks if a currency exists by code.
Sourcepub fn set_markets(
&self,
markets: Vec<Market>,
currencies: Option<Vec<Currency>>,
exchange_id: &str,
) -> Result<Arc<HashMap<String, Arc<Market>>>>
pub fn set_markets( &self, markets: Vec<Market>, currencies: Option<Vec<Currency>>, exchange_id: &str, ) -> Result<Arc<HashMap<String, Arc<Market>>>>
Sets market and currency data in the cache.
This operation clears existing data and replaces it with the new data. It’s designed to be called during market loading/refresh.