diff --git a/Cargo.lock b/Cargo.lock index 062cc7d080..5b81c6b85c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6115,7 +6115,6 @@ dependencies = [ "bad-tokens", "balance-overrides", "bigdecimal", - "cached", "chain", "chrono", "clap", @@ -6134,6 +6133,7 @@ dependencies = [ "mimalloc", "mockall", "model", + "moka", "multibase", "num", "number", diff --git a/crates/orderbook/Cargo.toml b/crates/orderbook/Cargo.toml index 22737e4cb4..8ebd925ac1 100644 --- a/crates/orderbook/Cargo.toml +++ b/crates/orderbook/Cargo.toml @@ -25,7 +25,6 @@ axum = { workspace = true } bad-tokens = { workspace = true } balance-overrides = { workspace = true } bigdecimal = { workspace = true } -cached = { workspace = true } chain = { workspace = true } chrono = { workspace = true, features = ["clock"] } clap = { workspace = true } @@ -43,6 +42,7 @@ humantime = { workspace = true } humantime-serde = { workspace = true } mimalloc = { workspace = true, optional = true } model = { workspace = true } +moka = { workspace = true } multibase = { workspace = true } num = { workspace = true } number = { workspace = true } diff --git a/crates/orderbook/src/ipfs_app_data.rs b/crates/orderbook/src/ipfs_app_data.rs index 34493cb4e8..1f2850cd89 100644 --- a/crates/orderbook/src/ipfs_app_data.rs +++ b/crates/orderbook/src/ipfs_app_data.rs @@ -2,13 +2,12 @@ use { crate::ipfs::Ipfs, anyhow::Result, app_data::{AppDataHash, create_ipfs_cid}, - cached::{Cached, TimedSizedCache}, - std::sync::Mutex, + moka::future::Cache, }; pub struct IpfsAppData { ipfs: Ipfs, - cache: Mutex>>, + cache: Cache>, metrics: &'static Metrics, } @@ -34,9 +33,10 @@ impl IpfsAppData { } Self { ipfs, - cache: Mutex::new(TimedSizedCache::with_size_and_lifespan_and_refresh( - 1000, 600, false, - )), + cache: Cache::builder() + .max_capacity(1000) + .time_to_live(std::time::Duration::from_secs(600)) + .build(), metrics, } } @@ -63,7 +63,7 @@ impl IpfsAppData { result } Ok(None) => { - tracing::debug!(?contract_app_data, %cid,"no full app data"); + tracing::debug!(?contract_app_data, %cid, "no full app data"); return Ok(None); } Err(err) => { @@ -86,37 +86,23 @@ impl IpfsAppData { pub async fn fetch(&self, contract_app_data: &AppDataHash) -> Result> { let outcome = |data: &Option| if data.is_some() { "found" } else { "missing" }; - let metric = &self.metrics.app_data; - if let Some(cached) = self - .cache - .lock() - .unwrap() - .cache_get(contract_app_data) - .cloned() - { + if let Some(cached) = self.cache.get(contract_app_data).await { metric.with_label_values(&[outcome(&cached), "cache"]).inc(); return Ok(cached); } - - let fetched = { - let _timer = self.metrics.fetches.start_timer(); - self.fetch_raw(contract_app_data).await - }; - let result = match fetched { - Ok(result) => result, - Err(err) => { + let _timer = self.metrics.fetches.start_timer(); + let result = self.fetch_raw(contract_app_data).await; + match &result { + Ok(result) => { + self.cache.insert(*contract_app_data, result.clone()).await; + metric.with_label_values(&[outcome(result), "node"]).inc(); + } + Err(_) => { metric.with_label_values(&["error", "node"]).inc(); - return Err(err); } - }; - - self.cache - .lock() - .unwrap() - .cache_set(*contract_app_data, result.clone()); - metric.with_label_values(&[outcome(&result), "node"]).inc(); - Ok(result) + } + result } }