fix(cache): preserve non-JSON container entries

This commit is contained in:
linhongyu510 2026-09-11 12:28:34 +08:00
parent 5daa555bb0
commit 785ae73276
2 changed files with 13 additions and 1 deletions

View file

@ -73,7 +73,10 @@ class InMemoryCache(BaseCache):
# Only convert to JSON if absolutely necessary
if not isinstance(value, (str, bytes)):
value = json.dumps(value, default=str)
try:
value = json.dumps(value, default=str)
except (TypeError, ValueError):
value = repr(value)
return sys.getsizeof(value) / 1024 <= self.max_size_per_item

View file

@ -84,6 +84,15 @@ def test_in_memory_cache_max_size_per_item_measures_container_contents():
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