use async_trait::async_trait;
use super::{BlockWindowCache, CacheKey, CacheStats};
use crate::blocks::window::DailyBlockWindow;
use crate::errors::BlockWindowError;
#[derive(Debug, Clone, Copy, Default)]
pub struct NoOpCache;
#[async_trait]
impl BlockWindowCache for NoOpCache {
async fn get(&self, _key: &CacheKey) -> Option<DailyBlockWindow> {
None
}
async fn insert(
&self,
_key: CacheKey,
_window: DailyBlockWindow,
) -> Result<(), BlockWindowError> {
Ok(())
}
async fn clear(&self) -> Result<(), BlockWindowError> {
Ok(())
}
async fn stats(&self) -> CacheStats {
CacheStats::default()
}
fn name(&self) -> &'static str {
"NoOpCache"
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_chains::NamedChain;
use chrono::NaiveDate;
#[tokio::test]
async fn test_noop_cache_always_misses() {
let cache = NoOpCache;
let key = CacheKey::new(
NamedChain::Arbitrum,
NaiveDate::from_ymd_opt(2025, 10, 15).unwrap(),
);
assert!(cache.get(&key).await.is_none());
}
#[tokio::test]
async fn test_noop_cache_ignores_writes() {
let cache = NoOpCache;
let key = CacheKey::new(
NamedChain::Arbitrum,
NaiveDate::from_ymd_opt(2025, 10, 15).unwrap(),
);
let window = DailyBlockWindow {
start_block: 1000,
end_block: 2000,
start_ts: crate::blocks::window::UnixTimestamp(1728518400),
end_ts_exclusive: crate::blocks::window::UnixTimestamp(1728604800),
};
assert!(cache.insert(key.clone(), window).await.is_ok());
assert!(cache.get(&key).await.is_none());
}
#[tokio::test]
async fn test_noop_cache_stats() {
let cache = NoOpCache;
let stats = cache.stats().await;
assert_eq!(stats.hits, 0);
assert_eq!(stats.misses, 0);
assert_eq!(stats.evictions, 0);
assert_eq!(stats.entries, 0);
}
#[tokio::test]
async fn test_noop_cache_clear() {
let cache = NoOpCache;
assert!(cache.clear().await.is_ok());
}
}