Skip to content

Commit 41d182c

Browse files
committed
remove logging config override, remove dead code, adjust evict_key logic, and avoid calling exists unnecessarily
1 parent 6861490 commit 41d182c

File tree

1 file changed

+6
-26
lines changed

1 file changed

+6
-26
lines changed

src/zarr/experimental/cache_store.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from zarr.abc.store import ByteRequest, Store
99
from zarr.storage._wrapper import WrapperStore
1010

11-
logging.basicConfig(level=logging.INFO)
1211
logger = logging.getLogger(__name__)
1312

1413
if TYPE_CHECKING:
@@ -115,11 +114,6 @@ def _is_key_fresh(self, key: str) -> bool:
115114
elapsed = now - self.key_insert_times.get(key, 0)
116115
return elapsed < self.max_age_seconds
117116

118-
def _get_cache_size(self, key: str) -> int:
119-
"""Get the size of a cached item."""
120-
# For now, we'll estimate by getting the data when we cache it
121-
return 0 # Will be properly set when caching
122-
123117
async def _accommodate_value(self, value_size: int) -> None:
124118
"""Ensure there is enough space in the cache for a new value."""
125119
if self.max_size is None:
@@ -132,28 +126,24 @@ async def _accommodate_value(self, value_size: int) -> None:
132126
await self._evict_key(lru_key)
133127

134128
async def _evict_key(self, key: str) -> None:
135-
"""Remove a key from cache and update size tracking."""
136129
try:
137-
# Get the size of the key being evicted
138130
key_size = self._key_sizes.get(key, 0)
131+
# Delete from cache store FIRST
132+
await self._cache.delete(key)
139133

140-
# Remove from tracking structures
134+
# Only update tracking after successful deletion
141135
if key in self._cache_order:
142136
del self._cache_order[key]
143137
if key in self.key_insert_times:
144138
del self.key_insert_times[key]
145139
if key in self._key_sizes:
146140
del self._key_sizes[key]
147141

148-
# Update current size
149142
self._current_size = max(0, self._current_size - key_size)
150-
151-
# Actually delete from cache store
152-
await self._cache.delete(key)
153-
154-
logger.info("_evict_key: evicted key %s from cache, size %d", key, key_size)
143+
logger.info("_evict_key: evicted key %s, freed %d bytes", key, key_size)
155144
except Exception as e:
156145
logger.warning("_evict_key: failed to evict key %s: %s", key, e)
146+
# Don't update tracking if deletion failed
157147

158148
async def _cache_value(self, key: str, value: Any) -> None:
159149
"""Cache a value with size tracking."""
@@ -209,17 +199,7 @@ async def _get_try_cache(
209199
logger.info("_get_try_cache: key %s found in cache", key)
210200
# Update access order for LRU
211201
self._update_access_order(key)
212-
# Verify the key still exists in source store before returning cached data
213-
if await super().exists(key):
214-
return maybe_cached_result
215-
else:
216-
# Key no longer exists in source, clean up cache
217-
logger.info(
218-
"_get_try_cache: key %s no longer exists in source, cleaning up cache", key
219-
)
220-
await self._cache.delete(key)
221-
self._remove_from_tracking(key)
222-
return None
202+
return maybe_cached_result
223203
else:
224204
logger.info("_get_try_cache: key %s not found in cache, fetching from store", key)
225205
maybe_fresh_result = await super().get(key, prototype, byte_range)

0 commit comments

Comments
 (0)