use depyler::converge::{
CacheConfig, CacheEntry, CompilationStatus, SqliteCache, TranspilationCacheKey,
};
use tempfile::TempDir;
fn main() -> anyhow::Result<()> {
println!("=================================================");
println!(" DEPYLER-CACHE-001: O(1) Compilation Cache Demo ");
println!("=================================================\n");
let temp_dir = TempDir::new()?;
let cache_dir = temp_dir.path().to_path_buf();
println!("1. Creating cache configuration...");
let config = CacheConfig {
cache_dir: cache_dir.clone(),
max_size_bytes: 100 * 1024 * 1024, max_age_secs: 7 * 24 * 60 * 60, min_entries: 10,
};
println!(" Cache directory: {}", cache_dir.display());
println!(" Max size: {} MB", config.max_size_bytes / (1024 * 1024));
println!(
" Max age: {} days\n",
config.max_age_secs / (24 * 60 * 60)
);
println!("2. Opening SQLite cache with CAS backend...");
let cache = SqliteCache::open(config.clone())?;
println!(" Cache opened successfully!\n");
println!("3. Creating hermetic cache key...");
let python_source = r#"
def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
"#;
let cache_key = TranspilationCacheKey::compute(python_source, &config);
println!(" Source hash: {}...", &cache_key.hex_key()[..16]);
println!(" Combined key: {}\n", cache_key.hex_key());
println!("4. Storing transpilation result in cache...");
let rust_code = r#"
pub fn fibonacci(n: i64) -> i64 {
if n <= 1 {
return n;
}
fibonacci(n - 1) + fibonacci(n - 2)
}
"#;
let cargo_toml = r#"
[package]
name = "fibonacci"
version = "0.1.0"
edition = "2021"
"#;
let entry = CacheEntry {
rust_code_blob: String::new(), cargo_toml_blob: String::new(), dependencies: vec!["std".to_string()],
status: CompilationStatus::Success,
error_messages: vec![],
created_at: 0,
last_accessed_at: 0,
transpilation_time_ms: 15,
};
cache.store(&cache_key, rust_code, cargo_toml, entry)?;
println!(" Entry stored successfully!\n");
println!("5. Looking up cache entry...");
match cache.lookup(&cache_key)? {
Some(cached) => {
println!(" CACHE HIT!");
println!(" Status: {:?}", cached.status);
println!(" Transpilation time: {}ms", cached.transpilation_time_ms);
println!(" Rust code blob: {}...\n", &cached.rust_code_blob[..16]);
}
None => {
println!(" CACHE MISS (unexpected)\n");
}
}
println!("6. Testing cache miss with different source...");
let different_key = TranspilationCacheKey::compute("def hello(): return 'world'", &config);
match cache.lookup(&different_key)? {
Some(_) => println!(" Unexpected hit!"),
None => println!(" CACHE MISS (expected - different source)\n"),
}
println!("7. Cache statistics...");
let stats = cache.stats()?;
println!(" Total entries: {}", stats.total_entries);
println!(" Cache hits: {}", stats.hit_count);
println!(" Cache misses: {}", stats.miss_count);
println!(" Hit rate: {:.1}%", stats.hit_rate());
println!(" Total size: {} bytes\n", stats.total_size_bytes);
println!("8. Running garbage collection...");
let gc_result = cache.gc()?;
println!(" Entries evicted: {}", gc_result.evicted);
println!(" Bytes freed: {}\n", gc_result.freed_bytes);
println!("=================================================");
println!(" Cache demo completed successfully!");
println!("=================================================");
println!("\nTry the CLI commands:");
println!(" depyler cache stats");
println!(" depyler cache gc --dry-run");
println!(" depyler cache clear --force");
Ok(())
}