FXC-3903: tidy3d CLI Cache Helpers #2959
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements cache CLI helpers:
Sidefix: Move cache directory instead of deleting it on change.
Note: Branch was originally coming from this one (#2945) as the cache stats from there are used, this is why greptile comments are partially misleading.
Greptile Overview
Updated On: 2025-11-04 15:12:18 UTC
Greptile Summary
This PR implements CLI helpers for the local cache system, along with a major refactoring to improve performance by introducing a lightweight stats tracking mechanism.
Key Changes:
tidy3d cache info(shows config and usage stats),tidy3d cache list(displays all cache entries), andtidy3d cache clear(removes all cached data)CacheStatsandCacheEntryMetadataPydantic models to replace loose dictionaries for better type safetystats.jsonfile to track cache metadata (last_used,total_size,total_entries) without needing to iterate through all cache entriesthreading.Lockwiththreading.RLockand added_with_lock()context manager for cleaner lock management__len__()method to read from stats file instead of iterating all entriessync_stats()method to rebuild stats file from actual cache entries when inconsistencies are detectedIssues Found:
tidy3d/web/cache.py) whereold_root._rootshould beold_rootConfidence Score: 4/5
old_root._rootshould beold_root, which would cause an AttributeError if that code path is executed. The rest of the code is solid with proper locking, atomic operations, and thorough test coverage.tidy3d/web/cache.pyline 800 - the syntax error must be fixed before mergingImportant Files Changed
File Analysis
Sequence Diagram
sequenceDiagram participant User participant CLI as CLI Commands participant Cache as LocalCache participant Stats as CacheStats participant FS as File System Note over User,FS: Cache Info Command User->>CLI: tidy3d cache info CLI->>Cache: sync_stats() Cache->>FS: Read stats.json FS-->>Cache: Return stats data Cache->>Cache: _iter_entries() to rebuild if needed Cache->>FS: Write updated stats.json FS-->>Cache: Stats written Cache-->>CLI: Return CacheStats CLI-->>User: Display info (enabled, entries, size, limits) Note over User,FS: Cache List Command User->>CLI: tidy3d cache list CLI->>Cache: list() Cache->>Cache: _with_lock() Cache->>Cache: _iter_entries() Cache->>FS: Read metadata from each entry FS-->>Cache: Entry metadata Cache-->>CLI: Return list of entries CLI-->>User: Display formatted entries Note over User,FS: Store Result with Stats Tracking User->>Cache: store_result(data, task_id, path, type) Cache->>Cache: _with_lock() Cache->>Cache: _ensure_limits() Cache->>Stats: _load_stats() Stats-->>Cache: Current stats Cache->>Cache: Check if eviction needed alt Eviction Required Cache->>Cache: _evict() or _evict_by_size() Cache->>FS: Remove old entries Cache->>Stats: _record_remove_stats() end Cache->>FS: Copy artifact to temp location Cache->>Cache: Calculate checksum Cache->>FS: Write metadata.json Cache->>FS: Atomic rename to final location Cache->>Stats: _record_store_stats() Stats->>FS: Write updated stats.json Cache-->>User: Entry stored Note over User,FS: Fetch with Stats Update User->>Cache: try_fetch(simulation) Cache->>Cache: _with_lock() Cache->>Cache: build_cache_key() Cache->>Cache: _fetch(key) Cache->>FS: Load metadata Cache->>Cache: verify() checksum Cache->>Cache: _touch(entry) Cache->>Stats: _record_touch_stats() Stats->>FS: Update stats.json with new last_used Cache-->>User: Return CacheEntry Note over User,FS: Clear Cache User->>CLI: tidy3d cache clear CLI->>Cache: clear() Cache->>FS: Remove cache directory Cache->>FS: Write empty stats.json CLI-->>User: "Local cache cleared."