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/4282.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A `CacheStore` opened over a cache store that already holds entries now registers an entry the first time it is hit, so inherited entries count toward `max_size` and can be evicted instead of staying untracked.
8 changes: 7 additions & 1 deletion src/zarr/experimental/cache_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ async def _get_try_cache(
maybe_cached = await self._cache.get(key, prototype)
if maybe_cached is not None:
self._state.hits += 1
await self._update_access_order(key)
if key in self._state.key_sizes:
await self._update_access_order(key)
else:
# Entry left by an earlier CacheStore over the same cache
# store; register it so it counts toward max_size and can
# be evicted (#4282).
await self._track_entry(key, maybe_cached)
return maybe_cached
else:
# Byte-range read — use in-memory range cache
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 @@ -473,6 +473,35 @@ async def test_value_exceeds_max_size(self) -> None:
assert info["cached_keys"] == 0
assert info["current_size"] == 0

async def test_hit_on_inherited_entry_registers_it(self) -> None:
"""A hit on an entry left by an earlier CacheStore over the same cache
store registers it for size accounting and eviction (#4282)."""
source_store = MemoryStore()
cache_store = MemoryStore()

first = CacheStore(source_store, cache_store=cache_store, max_size=100)
data = CPUBuffer.from_bytes(b"x" * 40)
await first.set("key1", data)
await first.set("key2", data)

# A new instance over the same cache store starts with empty tracking
second = CacheStore(source_store, cache_store=cache_store, max_size=100)
assert second.cache_info()["current_size"] == 0

# A hit on an inherited entry registers it
result = await second.get("key1", default_buffer_prototype())
assert result is not None
info = second.cache_info()
assert info["current_size"] == 40
assert info["cached_keys"] == 1

# The registered entry counts toward max_size, so further writes evict
# it as the least recently used entry
await second.set("key3", data)
await second.set("key4", data)
assert not await cache_store.exists("key1")
assert second.cache_info()["current_size"] <= 100

async def test_get_nonexistent_key(self) -> None:
"""Test getting a key that doesn't exist in either store."""
source_store = MemoryStore()
Expand Down