Skip to content

Commit 9560afb

Browse files
ryo33claude
andcommitted
Refactor cache to support multiple cache types
- Rename default_cache_dir() to base_cache_dir() (no subdirectory) - Add https_cache_dir() for HTTPS fetched content - Cache structure: base_cache_dir()/https/ for remote files - Future cache types can use base_cache_dir()/compile/, etc. - CLI `cache path` now shows base directory 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 3814c39 commit 9560afb

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

crates/eure-cli/src/commands/cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::path::PathBuf;
44

55
use clap::Subcommand;
66
use eure_env::cache::{
7-
self, CacheOptions, CacheStorage, FsStorage, GcOptions, clean, clean_with_dir,
8-
default_cache_dir, gc, gc_with_dir, parse_duration, parse_size,
7+
self, CacheOptions, CacheStorage, FsStorage, GcOptions, base_cache_dir, clean, clean_with_dir,
8+
gc, gc_with_dir, https_cache_dir, parse_duration, parse_size,
99
};
1010
use url::Url;
1111

@@ -75,7 +75,7 @@ pub fn run(args: Args) {
7575
}
7676

7777
fn run_list(cache_dir: Option<PathBuf>) {
78-
let dir = cache_dir.unwrap_or_else(default_cache_dir);
78+
let dir = cache_dir.unwrap_or_else(https_cache_dir);
7979
let storage = FsStorage::new(dir);
8080

8181
match storage.list() {
@@ -164,7 +164,7 @@ fn run_clean(cache_dir: Option<PathBuf>) {
164164
}
165165

166166
fn run_path() {
167-
println!("{}", default_cache_dir().display());
167+
println!("{}", base_cache_dir().display());
168168
}
169169

170170
fn run_fetch(url_str: String, refresh: bool, cache_dir: Option<PathBuf>) {

crates/eure-env/src/cache/fetch.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,39 @@ pub struct FetchResult {
5555
pub cache_path: Option<std::path::PathBuf>,
5656
}
5757

58-
/// Get the default cache directory.
59-
pub fn default_cache_dir() -> std::path::PathBuf {
58+
/// Get the base cache directory for all eure caches.
59+
///
60+
/// Platform-specific paths (via `directories` crate):
61+
/// - macOS: `~/Library/Caches/dev.eure.eure/`
62+
/// - Linux: `~/.cache/eure/`
63+
/// - Windows: `C:\Users\<User>\AppData\Local\eure\eure\cache\`
64+
///
65+
/// Override with `$EURE_CACHE_DIR` environment variable.
66+
pub fn base_cache_dir() -> std::path::PathBuf {
6067
std::env::var("EURE_CACHE_DIR")
6168
.map(std::path::PathBuf::from)
6269
.unwrap_or_else(|_| {
6370
directories::ProjectDirs::from("dev", "eure", "eure")
6471
.map(|p| p.cache_dir().to_path_buf())
6572
.unwrap_or_else(|| std::path::PathBuf::from(".cache/eure"))
66-
.join("schemas")
6773
})
6874
}
6975

76+
/// Get the cache directory for HTTPS fetched content.
77+
///
78+
/// Returns `base_cache_dir()/https/`.
79+
pub fn https_cache_dir() -> std::path::PathBuf {
80+
base_cache_dir().join("https")
81+
}
82+
7083
/// Fetch a URL with caching.
7184
pub fn fetch(url: &Url, opts: &CacheOptions) -> Result<FetchResult, CacheError> {
7285
// Check HTTPS requirement
7386
if url.scheme() == "http" && !opts.allow_http {
7487
return Err(CacheError::HttpsRequired(url.to_string()));
7588
}
7689

77-
let cache_dir = opts.cache_dir.clone().unwrap_or_else(default_cache_dir);
90+
let cache_dir = opts.cache_dir.clone().unwrap_or_else(https_cache_dir);
7891
let storage = FsStorage::new(cache_dir);
7992

8093
// Check cache first (unless refresh is set)

crates/eure-env/src/cache/gc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use std::time::Duration;
44

55
use super::error::CacheError;
6-
use super::fetch::default_cache_dir;
6+
use super::fetch::https_cache_dir;
77
use super::storage::{CacheStorage, FsStorage, GcOptions, GcStats};
88

9-
/// Run garbage collection on the cache.
9+
/// Run garbage collection on the HTTPS cache.
1010
///
1111
/// This removes old entries based on the provided options.
1212
pub fn gc(opts: &GcOptions) -> Result<GcStats, CacheError> {
13-
let cache_dir = default_cache_dir();
13+
let cache_dir = https_cache_dir();
1414
let storage = FsStorage::new(cache_dir);
1515
storage.gc(opts)
1616
}
@@ -21,9 +21,9 @@ pub fn gc_with_dir(cache_dir: &std::path::Path, opts: &GcOptions) -> Result<GcSt
2121
storage.gc(opts)
2222
}
2323

24-
/// Remove all cached entries.
24+
/// Remove all cached entries from the HTTPS cache.
2525
pub fn clean() -> Result<(), CacheError> {
26-
let cache_dir = default_cache_dir();
26+
let cache_dir = https_cache_dir();
2727
let storage = FsStorage::new(cache_dir);
2828
storage.clean()
2929
}

crates/eure-env/src/cache/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! Remote schema caching module.
1+
//! Caching module for eure.
22
//!
3-
//! This module provides caching for remote schema files fetched over HTTP(S).
3+
//! This module provides caching infrastructure with support for multiple cache types.
44
//!
55
//! # Module Organization
66
//!
@@ -10,21 +10,23 @@
1010
//! # Cache Layout
1111
//!
1212
//! The cache is stored in the platform-specific cache directory (via `directories` crate):
13-
//! - macOS: `~/Library/Caches/dev.eure.eure/schemas/`
14-
//! - Linux: `~/.cache/eure/schemas/`
15-
//! - Windows: `C:\Users\<User>\AppData\Local\eure\eure\cache\schemas\`
13+
//! - macOS: `~/Library/Caches/dev.eure.eure/`
14+
//! - Linux: `~/.cache/eure/`
15+
//! - Windows: `C:\Users\<User>\AppData\Local\eure\eure\cache\`
1616
//!
1717
//! Override with `$EURE_CACHE_DIR` environment variable.
1818
//!
19-
//! Files are organized with 2-level directory sharding to prevent overcrowding:
19+
//! Cache types are stored in subdirectories:
2020
//!
2121
//! ```text
22-
//! ~/Library/Caches/dev.eure.eure/schemas/ # (macOS example)
23-
//! eure.dev/
24-
//! a1/
25-
//! b2/
26-
//! a1b2c3d4-eure-schema.schema.eure # content
27-
//! a1b2c3d4-eure-schema.schema.eure.meta # metadata (JSON)
22+
//! ~/Library/Caches/dev.eure.eure/ # base_cache_dir() (macOS example)
23+
//! https/ # https_cache_dir() - HTTPS fetched content
24+
//! eure.dev/
25+
//! a1/
26+
//! b2/
27+
//! a1b2c3d4-schema.eure # content
28+
//! a1b2c3d4-schema.eure.meta # metadata (JSON)
29+
//! # (future: compile/, build/, etc.)
2830
//! ```
2931
//!
3032
//! # Example (native only)
@@ -61,7 +63,7 @@ mod storage;
6163
#[cfg(feature = "native")]
6264
pub use error::CacheError;
6365
#[cfg(feature = "native")]
64-
pub use fetch::{CacheOptions, FetchResult, default_cache_dir, fetch};
66+
pub use fetch::{CacheOptions, FetchResult, base_cache_dir, fetch, https_cache_dir};
6567
#[cfg(feature = "native")]
6668
pub use gc::{clean, clean_with_dir, gc, gc_with_dir, parse_duration, parse_size};
6769
#[cfg(feature = "native")]

crates/eure/src/query/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::query::error::EureQueryError;
1313
use super::TextFileContent;
1414

1515
#[cfg(feature = "native")]
16-
pub use eure_env::cache::{CacheOptions, default_cache_dir, parse_duration};
16+
pub use eure_env::cache::{CacheOptions, base_cache_dir, https_cache_dir, parse_duration};
1717

1818
/// Rate limit: minimum interval between requests to the same URL.
1919
const RATE_LIMIT_INTERVAL: Duration = Duration::from_secs(10);

crates/eure/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub use diagnostics::{DiagnosticMessage, DiagnosticSeverity, GetDiagnostics};
4242
#[cfg(feature = "http")]
4343
pub use http::fetch_url;
4444
#[cfg(feature = "native")]
45-
pub use http::{CacheOptions, default_cache_dir, fetch_url_cached, parse_duration};
45+
pub use http::{CacheOptions, base_cache_dir, fetch_url_cached, https_cache_dir, parse_duration};
4646
pub use parse::{ParseCst, ParseDocument, ParsedCst, ParsedDocument, ValidCst};
4747
#[cfg(feature = "http")]
4848
pub use reqwest;

0 commit comments

Comments
 (0)