diff --git a/cacholote/clean.py b/cacholote/clean.py index 578e8d8..b11b7e9 100644 --- a/cacholote/clean.py +++ b/cacholote/clean.py @@ -96,16 +96,16 @@ def __init__(self) -> None: self.logger = config.get().logger self.fs, self.dirname = utils.get_cache_files_fs_dirname() - urldir = self.fs.unstrip_protocol(self.dirname) + self.urldir = self.fs.unstrip_protocol(self.dirname) self.logger.info("getting disk usage") self.file_sizes: dict[str, int] = collections.defaultdict(int) for path, size in self.fs.du(self.dirname, total=False).items(): # Group dirs urlpath = self.fs.unstrip_protocol(path) - basename, *_ = urlpath.replace(urldir, "", 1).strip("/").split("/") + basename, *_ = urlpath.replace(self.urldir, "", 1).strip("/").split("/") if basename: - self.file_sizes[posixpath.join(urldir, basename)] += size + self.file_sizes[posixpath.join(self.urldir, basename)] += size self.disk_usage = sum(self.file_sizes.values()) self.log_disk_usage() @@ -254,16 +254,16 @@ def delete_cache_files( sa.select(database.CacheEntry).filter(*filters).order_by(*sorters) ): files = _get_files_from_cache_entry(cache_entry) - if files: + if any(file.startswith(self.urldir) for file in files): n_entries_to_delete += 1 session.delete(cache_entry) - for file, file_type in files.items(): - self.pop_file_size(file) - if file_type == "application/vnd+zarr": - dirs_to_delete.append(file) - else: - files_to_delete.append(file) + for file, file_type in files.items(): + self.pop_file_size(file) + if file_type == "application/vnd+zarr": + dirs_to_delete.append(file) + else: + files_to_delete.append(file) if self.stop_cleaning(maxsize): break diff --git a/tests/test_60_clean.py b/tests/test_60_clean.py index c27a8f4..8186ba1 100644 --- a/tests/test_60_clean.py +++ b/tests/test_60_clean.py @@ -349,3 +349,22 @@ def test_expire_cache_entries_created_at() -> None: assert clean.expire_cache_entries(after=toc) == 0 assert clean.expire_cache_entries(before=toc) == 1 assert clean.expire_cache_entries(after=tic) == 1 + + +def test_multiple(tmp_path: pathlib.Path) -> None: + oldpath = tmp_path / "old.txt" + oldpath.write_bytes(ONE_BYTE) + with config.set(cache_files_urlpath=str(tmp_path / "old")): + cached_oldpath = pathlib.Path(open_url(oldpath).path) + assert cached_oldpath.exists() + + newpath = tmp_path / "new.txt" + newpath.write_bytes(ONE_BYTE) + with config.set(cache_files_urlpath=str(tmp_path / "new")): + cached_newpath = pathlib.Path(open_url(newpath).path) + assert cached_newpath.exists() + + with config.set(cache_files_urlpath=str(tmp_path / "new")): + clean.clean_cache_files(0) + assert not cached_newpath.exists() + assert cached_oldpath.exists()