use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::{ProbeResult, probe};
#[derive(Debug, Clone, Default)]
pub struct ProbeCache {
cache: Arc<RwLock<HashMap<String, ProbeResult>>>,
}
impl ProbeCache {
pub fn new() -> Self {
Self::default()
}
pub async fn probe(&self, path: &str) -> anyhow::Result<ProbeResult> {
{
let cache = self.cache.read().await;
if let Some(result) = cache.get(path) {
return Ok(result.clone());
}
}
let result = probe(path).await?;
{
let mut cache = self.cache.write().await;
cache.insert(path.to_string(), result.clone());
}
Ok(result)
}
}