ngdp_cache/
lib.rs

1//! Generic caching functionality for NGDP components
2//!
3//! This crate provides a flexible caching system for NGDP-related data including:
4//! - Generic cache for arbitrary data
5//! - CDN content cache (config, data, patch, indices)
6//! - Ribbit response cache
7//! - Cached clients for Ribbit, TACT, and CDN protocols
8
9use std::path::{Path, PathBuf};
10
11pub mod cached_cdn_client;
12pub mod cached_ribbit_client;
13pub mod cached_tact_client;
14pub mod cdn;
15pub mod error;
16pub mod generic;
17pub mod hybrid_version_client;
18pub mod ribbit;
19pub mod stats;
20
21pub use cdn::CdnCache;
22pub use error::{Error, Result};
23pub use stats::{CacheReport, CacheStats, CacheStatsSnapshot};
24
25/// Get the base NGDP cache directory
26///
27/// Returns a path like:
28/// - Linux: `~/.cache/ngdp`
29/// - macOS: `~/Library/Caches/ngdp`
30/// - Windows: `C:\Users\{user}\AppData\Local\ngdp\cache`
31pub fn get_cache_dir() -> Result<PathBuf> {
32    dirs::cache_dir()
33        .ok_or(Error::CacheDirectoryNotFound)
34        .map(|dir| dir.join("ngdp"))
35}
36
37/// Ensure a directory exists, creating it if necessary
38pub(crate) async fn ensure_dir(path: &Path) -> Result<()> {
39    if tokio::fs::metadata(path).await.is_err() {
40        tokio::fs::create_dir_all(path).await?;
41    }
42    Ok(())
43}