1
1
from __future__ import annotations
2
2
3
3
import hashlib
4
+ import json
4
5
import logging
5
6
from collections .abc import Iterator
6
7
from typing import Any , Callable , TypedDict , TypeVar
@@ -67,8 +68,6 @@ def _safe_cache_key_for_binary_data(data: Any) -> str:
67
68
return data
68
69
else :
69
70
# For other types, try JSON serialization
70
- import json
71
-
72
71
return json .dumps (data , sort_keys = True , default = str )
73
72
except (UnicodeDecodeError , UnicodeEncodeError , TypeError , ValueError ):
74
73
# If we can't serialize it, create a hash of its string representation
@@ -83,28 +82,28 @@ def _create_obj_create_cache_key(req: tsi.ObjCreateReq) -> str:
83
82
val = obj_dict .pop ("val" , None )
84
83
85
84
# Serialize everything except val
86
- import json
87
-
88
85
base_key = json .dumps (obj_dict , sort_keys = True )
89
86
90
87
# Handle val separately as it might contain binary data
91
88
val_key = _safe_cache_key_for_binary_data (val )
92
89
93
- return f"{ base_key } |val:{ val_key } "
94
90
except Exception :
95
91
# Fallback to a hash of the entire request
96
92
return hashlib .sha256 (str (req ).encode ("utf-8" , errors = "ignore" )).hexdigest ()
93
+ else :
94
+ return f"{ base_key } |val:{ val_key } "
97
95
98
96
99
97
def _create_file_create_cache_key (req : tsi .FileCreateReq ) -> str :
100
98
"""Create a cache key for FileCreateReq that handles binary content safely."""
101
99
try :
102
100
# Create key from project_id and name, plus hash of content
103
101
content_hash = _safe_cache_key_for_binary_data (req .content )
104
- return f"project:{ req .project_id } |name:{ req .name } |content:{ content_hash } "
105
102
except Exception :
106
103
# Fallback to a hash of the entire request
107
104
return hashlib .sha256 (str (req ).encode ("utf-8" , errors = "ignore" )).hexdigest ()
105
+ else :
106
+ return f"project:{ req .project_id } |name:{ req .name } |content:{ content_hash } "
108
107
109
108
110
109
class CachingMiddlewareTraceServer (tsi .TraceServerInterface ):
0 commit comments