GitBaby/src/cache/config.rs
zhenyi 5d32290cf1 feat(cache): split cache module into submodules and wire invalidation hooks
Two-part change:

1) Submodule split of src/cache/ (was a single ~150-line mod.rs)
   - src/cache/config.rs: CacheConfig, defaults, default_cache_dir(),
     DEFAULT_NAME / DEFAULT_MEMORY_CAPACITY_BYTES /
     DEFAULT_DISK_CAPACITY_BYTES constants
   - src/cache/key.rs: CacheKey / CacheValue type aliases and key
     derivation helpers (rev -> oid, blob-exists negatives)
   - src/cache/store.rs: CacheStore — the foyer HybridCache wrapper
     (open / close / get / insert / remove / contains), all errors
     mapped to BabyError::Cache with op-tagged source
   - src/cache/pools.rs: CachePools — multi-store aggregator used by
     the global singleton; replaces the old monolithic CacheStore
   - src/cache/stats.rs: CacheStats + counter accessors
   - src/cache/invalidation.rs: hook_ref_updated / hook_blob_written
     (single-process invalidation hooks, fired from usecase impls)
   - src/cache/mod.rs: now just submodule re-exports of
     (config, global, invalidation, key, pools, stats, store)
   - src/cache/global.rs: GITBABY_CACHE singleton + init / try /
     shutdown now operate on Arc<CachePools> instead of Arc<CacheStore>

2) Cache integration in usecase modules
   - src/blob/usecase.rs: read paths go through cache.get / insert;
     invalidation hooks fire on write
   - src/commit/usecase.rs: commit-list results cached, invalidated on
     commit write
   - src/branch/usecase.rs: branch-list results cached, invalidated on
     ref update
   - src/refs/usecase.rs: lookup-cache + ref-update hook
   - src/tags/usecase.rs: tag-info cache + write hook

3) AGENTS.md
   - Document that cached serde payloads (CommitInfo, Vec<TreeEntry>,
     etc.) follow additive-only compatibility: never rename/remove a
     field, every new field gets #[serde(default)] so stale payloads
     still deserialize.
   - Note that the foyer cache is single-process: fixed on-disk dir
     per config + in-process invalidation hooks. Sharing across
     processes on the same repo can leave stale metadata; do not do
     that.

CI: cargo build + cargo test (61 passed) green.

BREAKING: the public cache surface moved. Anyone reaching into
cache::CacheStore directly should switch to cache::CachePools (or
cache::global::try_global_cache). The high-level helpers in
cache::global keep the same name; only the inner type changed.
2026-08-14 18:38:18 +08:00

92 lines
2.7 KiB
Rust

use std::borrow::Cow;
use std::path::PathBuf;
pub const DEFAULT_NAME: &str = "gitbaby-blob-cache";
pub const DEFAULT_MEMORY_CAPACITY_BYTES: usize = 64 * 1024 * 1024;
pub const DEFAULT_DISK_CAPACITY_BYTES: usize = 1024 * 1024 * 1024;
pub const DEFAULT_LFS_NAME: &str = "gitbaby-lfs-cache";
pub const DEFAULT_LFS_DISK_CAPACITY_BYTES: usize = 5 * 1024 * 1024 * 1024;
#[derive(Debug, Clone)]
pub struct CacheConfig {
pub memory_capacity_bytes: usize,
pub disk_capacity_bytes: usize,
pub disk_dir: PathBuf,
pub name: Cow<'static, str>,
pub lfs: Option<LfsCacheConfig>,
}
#[derive(Debug, Clone)]
pub struct LfsCacheConfig {
pub memory_capacity_bytes: usize,
pub disk_capacity_bytes: usize,
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
memory_capacity_bytes: DEFAULT_MEMORY_CAPACITY_BYTES,
disk_capacity_bytes: DEFAULT_DISK_CAPACITY_BYTES,
disk_dir: default_cache_dir(),
name: Cow::Borrowed(DEFAULT_NAME),
lfs: None,
}
}
}
impl Default for LfsCacheConfig {
fn default() -> Self {
Self {
memory_capacity_bytes: DEFAULT_MEMORY_CAPACITY_BYTES,
disk_capacity_bytes: DEFAULT_LFS_DISK_CAPACITY_BYTES,
}
}
}
impl CacheConfig {
pub fn from_env() -> Self {
Self {
memory_capacity_bytes: env_usize(
"GITBABY_CACHE_MEM_BYTES",
DEFAULT_MEMORY_CAPACITY_BYTES,
),
disk_capacity_bytes: env_usize("GITBABY_CACHE_DISK_BYTES", DEFAULT_DISK_CAPACITY_BYTES),
disk_dir: std::env::var_os("GITBABY_CACHE_DIR")
.map(PathBuf::from)
.unwrap_or_else(default_cache_dir),
name: Cow::Borrowed(DEFAULT_NAME),
lfs: match env_usize(
"GITBABY_CACHE_LFS_DISK_BYTES",
DEFAULT_LFS_DISK_CAPACITY_BYTES,
) {
0 => None,
disk_capacity_bytes => Some(LfsCacheConfig {
memory_capacity_bytes: DEFAULT_MEMORY_CAPACITY_BYTES,
disk_capacity_bytes,
}),
},
}
}
}
fn env_usize(key: &str, default: usize) -> usize {
std::env::var(key)
.ok()
.and_then(|v| v.trim().parse::<usize>().ok())
.unwrap_or(default)
}
fn default_cache_dir() -> PathBuf {
if let Some(xdg) = std::env::var_os("XDG_CACHE_HOME") {
return PathBuf::from(xdg).join("gitbaby");
}
if let Some(local) = std::env::var_os("LOCALAPPDATA") {
return PathBuf::from(local).join("gitbaby");
}
if let Some(home) = std::env::var_os("HOME") {
return PathBuf::from(home).join(".cache").join("gitbaby");
}
std::env::temp_dir().join("gitbaby")
}