This commit is contained in:
moyai-berriai[bot] 2026-11-05 19:38:24 +00:00 • committed by GitHub
commit 8152ba9d1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View file

@ -62,13 +62,12 @@ class InMemoryCache(BaseCache):
try:
# Fast path for common primitive types that are typically small
if (
isinstance(value, (bool, int, float, str))
isinstance(value, (bool, int, float))
and len(str(value)) < self.max_size_per_item * MAX_SIZE_PER_ITEM_IN_MEMORY_CACHE_IN_KB
): # Conservative estimate
return True
# Direct size check for bytes objects
if isinstance(value, bytes):
if isinstance(value, (str, bytes)):
return sys.getsizeof(value) / 1024 <= self.max_size_per_item
# Handle special types without full conversion when possible

View file

@ -117,6 +117,28 @@ def test_in_memory_cache_max_size_per_item():
assert result is False
@pytest.mark.parametrize(
("character", "count", "should_cache"),
[
("a", 100, True),
("\u00e9", 100, True),
("\u6f22", 100, True),
("\U0001f600", 100, True),
("a", 1023, False),
("\u00e9", 1023, False),
("\u6f22", 600, False),
("\U0001f600", 300, False),
],
)
def test_string_cache_admission_respects_byte_size(character: str, count: int, should_cache: bool) -> None:
cache: Final = InMemoryCache(max_size_per_item=1, clock=lambda: 1000.0)
value: Final = character * count
cache.set_cache("text", value)
assert cache.get_cache("text") == (value if should_cache else None)
def test_in_memory_cache_ttl():
"""
Check that