@@ -214,27 +214,40 @@ def set_expire(
214
214
)
215
215
216
216
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
+ """
218
224
validate_key (key )
219
225
validate_cache (cache )
220
226
validate_tags (tags )
221
227
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 )
226
233
227
- return _val
234
+ return _val
228
235
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
+ )
233
240
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 :
235
248
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 },
238
251
}
239
252
240
253
0 commit comments