From a4bcdb7d615919c53eedf86c2dd92bbea511d87d Mon Sep 17 00:00:00 2001 From: allen0099 Date: Sat, 26 Sep 2026 13:33:14 +0000 Subject: [PATCH] refactor(manager): encode the JSON content once in CacheManager._encode _encode encoded the JSON string once for the fingerprint and again for the stored content. Encode it once and hash the same bytes. str.encode() already defaults to UTF-8, so the stored entry is unchanged. Closes #183 --- fastapi_cachex/manager.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fastapi_cachex/manager.py b/fastapi_cachex/manager.py index 7b935fc..3a86010 100644 --- a/fastapi_cachex/manager.py +++ b/fastapi_cachex/manager.py @@ -55,9 +55,9 @@ def _cache_key(self, key: str) -> str: @staticmethod def _encode(value: Any) -> CacheEntry: - json_content = json.dumps(value) - fingerprint = hashlib.sha256(json_content.encode()).hexdigest() - return CacheEntry(fingerprint=fingerprint, content=json_content.encode("utf-8")) + content = json.dumps(value).encode("utf-8") + fingerprint = hashlib.sha256(content).hexdigest() + return CacheEntry(fingerprint=fingerprint, content=content) async def get(self, key: str, default: Any = None) -> Any: """Retrieve and JSON-decode a cached value.