Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/0000.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Release the size of a `CacheStore` entry from `cache_info()['current_size']` when the entry is deleted, overwritten without caching, or found missing in the source store, so deleted entries no longer count toward `max_size` and force early evictions.
4 changes: 2 additions & 2 deletions src/zarr/experimental/cache_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ def _remove_from_tracking(self, entry_key: _CacheEntryKey) -> None:
"""
self._state.cache_order.pop(entry_key, None)
self._state.key_insert_times.pop(entry_key, None)
self._state.key_sizes.pop(entry_key, None)
entry_size = self._state.key_sizes.pop(entry_key, 0)
self._state.current_size = max(0, self._state.current_size - entry_size)

def _invalidate_range_entries(self, key: str) -> None:
"""Remove all byte-range entries for *key* from the range cache and tracking.
Expand Down Expand Up @@ -425,7 +426,6 @@ async def delete_dir(self, prefix: str) -> None:
self._invalidate_range_entries(key)
for entry_key in list(self._state.key_sizes):
if isinstance(entry_key, str) and entry_key.startswith(prefix):
self._state.current_size -= self._state.key_sizes[entry_key]
self._remove_from_tracking(entry_key)

def cache_info(self) -> dict[str, Any]:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_experimental/test_cache_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,35 @@ async def test_delete_dir_preserves_shared_cache_and_size_budget(self, tmp_path:
assert not await cached._cache.exists("database/chunk")
assert ("database/chunk", byte_range) in cached._state.cache_order

async def test_delete_releases_size_budget(self) -> None:
cached = CacheStore(MemoryStore(), cache_store=MemoryStore(), max_size=100)
await cached.set("a", CPUBuffer.from_bytes(b"x" * 60))
assert cached.cache_info()["current_size"] == 60

await cached.delete("a")
assert cached.cache_info()["current_size"] == 0

# The freed space is usable without evicting anything.
await cached.set("b", CPUBuffer.from_bytes(b"x" * 60))
await cached.set("c", CPUBuffer.from_bytes(b"x" * 30))
assert cached.cache_info()["current_size"] == 90
assert cached.cache_stats()["evictions"] == 0
assert await cached._cache.exists("b")

async def test_set_without_caching_releases_size_budget(self) -> None:
cached = CacheStore(
MemoryStore(), cache_store=MemoryStore(), max_size=100, cache_set_data=False
)
proto = default_buffer_prototype()
await cached._store.set("a", CPUBuffer.from_bytes(b"x" * 60))
await cached.get("a", proto)
assert cached.cache_info()["current_size"] == 60

# Overwriting drops the cached copy, so its size is no longer counted.
await cached.set("a", CPUBuffer.from_bytes(b"y" * 60))
assert not await cached._cache.exists("a")
assert cached.cache_info()["current_size"] == 0

async def test_delete_dir_empty_invalidates_both_caches(self) -> None:
source = MemoryStore()
cached = CacheStore(source, cache_store=MemoryStore())
Expand Down