This commit is contained in:
hylin 2026-11-06 19:29:33 -08:00 • committed by GitHub
commit 13c19bf5e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View file

@ -71,11 +71,6 @@ class InMemoryCache(BaseCache):
if isinstance(value, bytes):
return sys.getsizeof(value) / 1024 <= self.max_size_per_item
# Handle special types without full conversion when possible
if hasattr(value, "__sizeof__"): # Use __sizeof__ if available
size: Final = value.__sizeof__() / 1024
return size <= self.max_size_per_item
# Fallback for complex types
if isinstance(value, BaseModel) and hasattr(value, "model_dump"): # Pydantic v2
value = value.model_dump()
@ -84,7 +79,10 @@ class InMemoryCache(BaseCache):
# Only convert to JSON if absolutely necessary
if not isinstance(value, (str, bytes)):
value = json.dumps(value, default=str)
try:
return sys.getsizeof(json.dumps(value, default=str)) / 1024 <= self.max_size_per_item
except (TypeError, ValueError):
return sys.getsizeof(repr(value)) / 1024 <= self.max_size_per_item
return sys.getsizeof(value) / 1024 <= self.max_size_per_item

View file

@ -117,6 +117,27 @@ def test_in_memory_cache_max_size_per_item():
assert result is False
def test_in_memory_cache_max_size_per_item_measures_container_contents():
in_memory_cache = InMemoryCache(max_size_per_item=1)
oversized_value = {"timestamp": 0.0, "response": "a" * 5000}
small_value = {"timestamp": 0.0, "response": "ok"}
in_memory_cache.set_cache(key="oversized", value=oversized_value)
in_memory_cache.set_cache(key="small", value=small_value)
assert in_memory_cache.get_cache(key="oversized") is None
assert in_memory_cache.get_cache(key="small") == small_value
def test_in_memory_cache_accepts_small_container_with_non_json_keys():
in_memory_cache = InMemoryCache(max_size_per_item=1)
value = {("key", "key-hash"): ("job-1",)}
in_memory_cache.set_cache(key="active-jobs", value=value)
assert in_memory_cache.get_cache(key="active-jobs") == value
def test_in_memory_cache_ttl():
"""
Check that