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 ribbit;
18
19pub use cdn::CdnCache;
20pub use error::{Error, Result};
21
22/// Get the base NGDP cache directory
23///
24/// Returns a path like:
25/// - Linux: `~/.cache/ngdp`
26/// - macOS: `~/Library/Caches/ngdp`
27/// - Windows: `C:\Users\{user}\AppData\Local\ngdp\cache`
28pub fn get_cache_dir() -> Result<PathBuf> {
29 dirs::cache_dir()
30 .ok_or(Error::CacheDirectoryNotFound)
31 .map(|dir| dir.join("ngdp"))
32}
33
34/// Ensure a directory exists, creating it if necessary
35pub(crate) async fn ensure_dir(path: &Path) -> Result<()> {
36 if tokio::fs::metadata(path).await.is_err() {
37 tokio::fs::create_dir_all(path).await?;
38 }
39 Ok(())
40}