|
| 1 | +# devenv-cache-core |
| 2 | + |
| 3 | +A shared library for file tracking and caching in devenv. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This crate provides common functionality used by both the task cache and eval cache implementations: |
| 8 | + |
| 9 | +- File content hashing and modification detection |
| 10 | +- SQLite database connection and management |
| 11 | +- Time utilities for SystemTime ⟷ Unix timestamp conversion |
| 12 | +- Common error types |
| 13 | + |
| 14 | +## Components |
| 15 | + |
| 16 | +### Database Module (`db.rs`) |
| 17 | + |
| 18 | +Provides a configurable SQLite database connection manager with optimized settings: |
| 19 | + |
| 20 | +```rust |
| 21 | +// Create a database connection |
| 22 | +let config = DbConfig { |
| 23 | + path: PathBuf::from("/path/to/cache.db"), |
| 24 | + ..Default::default() |
| 25 | +}; |
| 26 | +let db = Database::new(config).await?; |
| 27 | + |
| 28 | +// Initialize schema |
| 29 | +db.init_schema(MY_SCHEMA).await?; |
| 30 | +``` |
| 31 | + |
| 32 | +### File Tracking (`file.rs`) |
| 33 | + |
| 34 | +Tracks file modifications using both modification time and content hashing: |
| 35 | + |
| 36 | +```rust |
| 37 | +// Track a file |
| 38 | +let file = TrackedFile::new(path)?; |
| 39 | + |
| 40 | +// Check if modified |
| 41 | +if file.is_modified()? { |
| 42 | + // File changed since last check |
| 43 | +} |
| 44 | + |
| 45 | +// Update tracked state |
| 46 | +file.update()?; |
| 47 | +``` |
| 48 | + |
| 49 | +### Time Utilities (`time.rs`) |
| 50 | + |
| 51 | +Convert between SystemTime and Unix timestamps: |
| 52 | + |
| 53 | +```rust |
| 54 | +let unix_time = system_time_to_unix_seconds(SystemTime::now()); |
| 55 | +let system_time = system_time_from_unix_seconds(unix_time); |
| 56 | +``` |
| 57 | + |
| 58 | +### Error Handling (`error.rs`) |
| 59 | + |
| 60 | +Common error types with thiserror and miette integration: |
| 61 | + |
| 62 | +```rust |
| 63 | +#[derive(Error, Diagnostic, Debug)] |
| 64 | +pub enum CacheError { |
| 65 | + #[error("Database error: {0}")] |
| 66 | + Database(#[from] sqlx::Error), |
| 67 | + |
| 68 | + #[error("I/O error: {0}")] |
| 69 | + Io(#[from] std::io::Error), |
| 70 | + |
| 71 | + // Other error variants... |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Integration Strategy |
| 76 | + |
| 77 | +### For Task Cache |
| 78 | + |
| 79 | +The task cache can use this shared library for file tracking and database operations, while keeping its own schema and business logic: |
| 80 | + |
| 81 | +1. Replace direct SQLite usage with the `Database` type |
| 82 | +2. Replace file monitoring with `TrackedFile` |
| 83 | +3. Use shared error types and time utilities |
| 84 | + |
| 85 | +### For Eval Cache |
| 86 | + |
| 87 | +Similarly, the eval cache can leverage the shared library while maintaining its specific file input tracking: |
| 88 | + |
| 89 | +1. Use the shared `Database` type for connection management |
| 90 | +2. Leverage `TrackedFile` for file state tracking |
| 91 | +3. Keep specialized command caching logic |
| 92 | + |
| 93 | +## Benefits |
| 94 | + |
| 95 | +- **Reduced Code Duplication**: Core functionality defined once |
| 96 | +- **Consistent Behavior**: Same file change detection logic across caches |
| 97 | +- **Simplified Maintenance**: Bug fixes in one place |
| 98 | +- **Better Abstraction**: Clear separation of concerns |
0 commit comments