feat(operations): cargo cache force purge ("empty all")
purge_cargo_cache gains force: bool. The default age-based purge only ever removed entries past the configured retention window, which read as a lie when a user expected "purge" to clear everything -- force=True sets the cutoff a day in the future so every existing entry is treated as expired, backing the web UI's explicit "Empty all" action. A disabled cache stays untouched either way.
This commit is contained in:
@@ -94,19 +94,33 @@ def cargo_cache_status(config: Config) -> CacheStatus:
|
||||
|
||||
|
||||
def purge_cargo_cache(
|
||||
config: Config, *, retention_days: int | None = None, dry_run: bool = False
|
||||
config: Config,
|
||||
*,
|
||||
retention_days: int | None = None,
|
||||
force: bool = False,
|
||||
dry_run: bool = False,
|
||||
) -> PurgeResult:
|
||||
"""Remove cache entries older than ``retention_days`` (or config's).
|
||||
|
||||
Sizes the victims before deleting (a real prune makes their bytes
|
||||
unreadable), so ``freed_bytes`` is accurate even for a real purge.
|
||||
A disabled cache is left untouched and reports an all-zero result.
|
||||
|
||||
``force=True`` ignores the retention window entirely and empties the whole
|
||||
cache (every entry, regardless of age) -- this backs the web UI's explicit
|
||||
"Empty all" action, as opposed to the default age-based purge. A disabled
|
||||
cache is still left untouched even under ``force``.
|
||||
"""
|
||||
if not config.cargo_cache.enabled:
|
||||
return PurgeResult(removed_entries=0, freed_bytes=0, dry_run=dry_run)
|
||||
|
||||
days = retention_days if retention_days is not None else config.cargo_cache.retention_days
|
||||
cutoff = datetime.now(tz=UTC) - timedelta(days=days)
|
||||
if force:
|
||||
# A cutoff in the future makes every existing entry (mtime <= now)
|
||||
# older than it, so the age-based prune removes the entire cache.
|
||||
cutoff = datetime.now(tz=UTC) + timedelta(days=1)
|
||||
else:
|
||||
days = retention_days if retention_days is not None else config.cargo_cache.retention_days
|
||||
cutoff = datetime.now(tz=UTC) - timedelta(days=days)
|
||||
store = CargoCacheStore(config.cargo_cache.root)
|
||||
|
||||
doomed = store.prune_older_than(cutoff, what_if=True)
|
||||
|
||||
Reference in New Issue
Block a user