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.
§Recommended TTLs
| Data | TTL |
|---|---|
| Instrument list / exchange info | 1 hour |
| Symbol precision / lot size | 1 hour |
| Server time | 30 sec |
| Recent ticker (REST polling fallback) | 10 sec |
| Funding rate | 5 min |
| Open interest | 30 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§
- Rest
Cache - Concurrent TTL cache.