Skip to content

Commit d20843a

Browse files
committed
Fixed eviction key logic with proper size tracking
1 parent cda4767 commit d20843a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/zarr/storage/_caching_store.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class CacheStore(WrapperStore[Store]):
7878
cache_set_data: bool
7979
_cache_order: OrderedDict[str, None] # Track access order for LRU
8080
_current_size: int # Track current cache size
81+
_key_sizes: dict[str, int] # Track size of each cached key
8182

8283
def __init__(
8384
self,
@@ -106,6 +107,7 @@ def __init__(
106107
self.cache_set_data = cache_set_data
107108
self._cache_order = OrderedDict()
108109
self._current_size = 0
110+
self._key_sizes = {}
109111

110112
def _is_key_fresh(self, key: str) -> bool:
111113
"""Check if a cached key is still fresh based on max_age_seconds."""
@@ -135,14 +137,20 @@ def _accommodate_value(self, value_size: int) -> None:
135137
def _evict_key(self, key: str) -> None:
136138
"""Remove a key from cache and update size tracking."""
137139
try:
138-
# Remove from cache store (async operation, but we'll handle it)
139-
# For now, we'll mark it for removal and actual removal happens in async methods
140+
# Get the size of the key being evicted
141+
key_size = self._key_sizes.get(key, 0)
142+
143+
# Remove from tracking structures
140144
if key in self._cache_order:
141145
del self._cache_order[key]
142146
if key in self.key_insert_times:
143147
del self.key_insert_times[key]
144-
# Note: Actual size reduction will happen when we get the item size
145-
logger.info("_evict_key: evicted key %s from cache", key)
148+
if key in self._key_sizes:
149+
del self._key_sizes[key]
150+
151+
# Update current size
152+
self._current_size = max(0, self._current_size - key_size)
153+
logger.info("_evict_key: evicted key %s from cache, size %d", key, key_size)
146154
except Exception as e:
147155
logger.warning("_evict_key: failed to evict key %s: %s", key, e)
148156

@@ -165,6 +173,7 @@ def _cache_value(self, key: str, value: Any) -> None:
165173
# Update tracking
166174
self._cache_order[key] = None # OrderedDict to track access order
167175
self._current_size += value_size
176+
self._key_sizes[key] = value_size
168177
self.key_insert_times[key] = time.monotonic()
169178

170179
logger.info("_cache_value: cached key %s with size %d bytes", key, value_size)
@@ -181,6 +190,8 @@ def _remove_from_tracking(self, key: str) -> None:
181190
del self._cache_order[key]
182191
if key in self.key_insert_times:
183192
del self.key_insert_times[key]
193+
if key in self._key_sizes:
194+
del self._key_sizes[key]
184195

185196
async def _get_try_cache(
186197
self, key: str, prototype: BufferPrototype, byte_range: ByteRequest | None = None
@@ -318,5 +329,6 @@ async def clear_cache(self) -> None:
318329
# Reset tracking
319330
self.key_insert_times.clear()
320331
self._cache_order.clear()
332+
self._key_sizes.clear()
321333
self._current_size = 0
322334
logger.info("clear_cache: cleared all cache data")

0 commit comments

Comments
 (0)