This commit is contained in:
Abdullah Alqahtani 2026-11-06 15:50:56 -05:00 • committed by GitHub
commit 03846ab5bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View file

@ -62,7 +62,7 @@ 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

View file

@ -392,3 +392,14 @@ async def test_async_increment_pipeline_keeps_each_key_for_its_own_ttl() -> None
assert cache.get_cache("minute") is None
assert cache.get_cache("day") == 1
@pytest.mark.parametrize("character", ("Ā", "界", "😀"))
def test_cache_rejects_unicode_strings_exceeding_the_byte_limit(character: str) -> None:
cache: Final = InMemoryCache(max_size_per_item=1, clock=lambda: 100.0)
oversized: Final = character * 700
assert sys.getsizeof(oversized) > 1024
cache.set_cache("large", oversized)
assert cache.get_cache("large") is None
cache.set_cache("small", character * 10)
assert cache.get_cache("small") == character * 10