1use std::collections::HashMap;
2use std::sync::Arc;
3use tokio::sync::RwLock;
4
5use crate::{ProbeResult, probe};
6
7#[derive(Debug, Clone, Default)]
9pub struct ProbeCache {
10 cache: Arc<RwLock<HashMap<String, ProbeResult>>>,
11}
12
13impl ProbeCache {
14 pub fn new() -> Self {
15 Self::default()
16 }
17
18 pub async fn probe(&self, path: &str) -> anyhow::Result<ProbeResult> {
20 {
22 let cache = self.cache.read().await;
23 if let Some(result) = cache.get(path) {
24 return Ok(result.clone());
25 }
26 }
27
28 let result = probe(path).await?;
30 {
31 let mut cache = self.cache.write().await;
32 cache.insert(path.to_string(), result.clone());
33 }
34
35 Ok(result)
36 }
37}