Skip to content

Commit 6e14b05

Browse files
committed
update
1 parent 64eee63 commit 6e14b05

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

src/cachier/cores/redis.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ def set_func(self, func):
7373
super().set_func(func)
7474
self._func_str = _get_func_str(func)
7575

76+
@staticmethod
77+
def _loading_pickle(raw_value) -> Any:
78+
"""Helper to load pickled data with some recovery attempts."""
79+
try:
80+
if isinstance(raw_value, bytes):
81+
return pickle.loads(raw_value)
82+
elif isinstance(raw_value, str):
83+
# try to recover by encoding; prefer utf-8 but fall
84+
# back to latin-1 in case raw binary was coerced to str
85+
try:
86+
return pickle.loads(raw_value.encode("utf-8"))
87+
except Exception:
88+
return pickle.loads(raw_value.encode("latin-1"))
89+
else:
90+
# unexpected type; attempt pickle.loads directly
91+
try:
92+
return pickle.loads(raw_value)
93+
except Exception:
94+
return None
95+
except Exception as exc:
96+
warnings.warn(
97+
f"Redis value deserialization failed: {exc}",
98+
stacklevel=2,
99+
)
100+
return None
101+
76102
def get_entry_by_key(self, key: str) -> Tuple[str, Optional[CacheEntry]]:
77103
"""Get entry based on given key from Redis."""
78104
redis_client = self._resolve_redis_client()
@@ -96,27 +122,7 @@ def _raw(field: str):
96122
value = None
97123
raw_value = _raw("value")
98124
if raw_value is not None:
99-
try:
100-
if isinstance(raw_value, bytes):
101-
value = pickle.loads(raw_value)
102-
elif isinstance(raw_value, str):
103-
# try to recover by encoding; prefer utf-8 but fall
104-
# back to latin-1 in case raw binary was coerced to str
105-
try:
106-
value = pickle.loads(raw_value.encode("utf-8"))
107-
except Exception:
108-
value = pickle.loads(raw_value.encode("latin-1"))
109-
else:
110-
# unexpected type; attempt pickle.loads directly
111-
try:
112-
value = pickle.loads(raw_value)
113-
except Exception:
114-
value = None
115-
except Exception as exc:
116-
warnings.warn(
117-
f"Redis value deserialization failed: {exc}",
118-
stacklevel=2,
119-
)
125+
value = self._loading_pickle(raw_value)
120126

121127
# Parse timestamp
122128
raw_ts = _raw("timestamp") or b""

0 commit comments

Comments
 (0)