Skip to content

Commit 6c5fe65

Browse files
redjaxredjax
and
redjax
authored
hotfix(cache-utils): Fix get_val function (#253)
Co-authored-by: redjax <[email protected]>
1 parent d52cfcd commit 6c5fe65

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

red_utils/diskcache_utils/operations.py

+26-13
Original file line numberDiff line numberDiff line change
@@ -214,27 +214,40 @@ def set_expire(
214214
)
215215

216216

217-
def get_val(key: valid_key_types = None, cache: Cache = None, tags: list[str] = None):
217+
def get_val(key: str = None, cache: Cache = None, tags: list[str] = None):
218+
"""Search for a key in a given cache.
219+
220+
Pass a diskcache.Cache object for cache, and a key (and optionally a list of tags).
221+
Function will search the cache and return a value if found, or a structured
222+
error dict describing the lack of key.
223+
"""
218224
validate_key(key)
219225
validate_cache(cache)
220226
validate_tags(tags)
221227

222-
if check_cache_key_exists(key=key, cache=cache):
223-
try:
224-
with cache as ref:
225-
_val = ref.get(key=key)
228+
try:
229+
if check_cache_key_exists(key=key, cache=cache):
230+
try:
231+
with cache as ref:
232+
_val = ref.get(key=key)
226233

227-
return _val
234+
return _val
228235

229-
except Exception as exc:
230-
raise Exception(
231-
f"Unhandled exception retrieving value of key [{key}]. Details: {exc}"
232-
)
236+
except Exception as exc:
237+
raise Exception(
238+
f"Unhandled exception retrieving value of key [{key}]. Details: {exc}"
239+
)
233240

234-
else:
241+
else:
242+
return {
243+
"error": "Key not found in cache",
244+
"details": {"key": key, "cache_dir": cache.directory},
245+
}
246+
247+
except Exception as exc:
235248
return {
236-
"error": "Missing key",
237-
"details": {"key": key, "cache_dir": cache.directory},
249+
"error": "Error searching for key in cache",
250+
"details": {"exception": exc},
238251
}
239252

240253

0 commit comments

Comments
 (0)