Skip to main content

Module rest_cache

Module rest_cache 

Source
Expand description

Generic TTL cache for REST responses (instrument metadata, exchange info, etc).

Wraps any async fn, caches by key. Per-key TTL. Concurrent-safe via DashMap.

DataTTL
Instrument list / exchange info1 hour
Symbol precision / lot size1 hour
Server time30 sec
Recent ticker (REST polling fallback)10 sec
Funding rate5 min
Open interest30 sec

These are suggestions — consumers choose their own TTL at construction time.

§Example

let cache: RestCache<String, String> = RestCache::new(Duration::from_secs(3600));
let info = cache.get_or_fetch("binance".to_string(), || async {
    Ok::<String, String>("exchange_info_payload".to_string())
}).await?;

§Single-flight note

get_or_fetch is not single-flight: if two tasks call it concurrently on the same missing key, both will invoke the loader. The second result simply overwrites the first. For most REST metadata use-cases (slow changing, cheap on race) this is acceptable. If strict single-flight is required, layer a tokio::sync::Mutex per key on top.

Structs§

RestCache
Concurrent TTL cache.