Skip to content

Commit c5477a8

Browse files
committed
Enhance caching validation and error handling in VFBquery functions
1 parent b53961a commit c5477a8

3 files changed

Lines changed: 113 additions & 19 deletions

File tree

src/vfbquery/cached_functions.py

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
from typing import Dict, Any, Optional
99
from .cache_enhancements import cache_result, get_cache
10+
11+
12+
def is_valid_term_info_result(result):
13+
"""Check if a term_info result has the essential fields"""
14+
if not result or not isinstance(result, dict):
15+
return False
16+
# Check for essential fields
17+
return result.get('Id') and result.get('Name')
1018
from .vfb_queries import (
1119
get_term_info as _original_get_term_info,
1220
get_instances as _original_get_instances,
@@ -57,8 +65,24 @@ def get_term_info_cached(short_form: str, preview: bool = False):
5765
# Check for complete result in cache first
5866
cache_key = cache._generate_cache_key("term_info_complete", short_form, preview)
5967
cached_result = cache.get(cache_key)
68+
print(f"DEBUG: Cache lookup for {short_form}: {'HIT' if cached_result is not None else 'MISS'}")
6069
if cached_result is not None:
61-
return cached_result
70+
# Validate that cached result has essential fields
71+
if not is_valid_term_info_result(cached_result):
72+
print(f"DEBUG: Cached result incomplete for {short_form}, falling back to original function")
73+
print(f"DEBUG: cached_result keys: {list(cached_result.keys()) if cached_result else 'None'}")
74+
print(f"DEBUG: cached_result Id: {cached_result.get('Id', 'MISSING') if cached_result else 'None'}")
75+
print(f"DEBUG: cached_result Name: {cached_result.get('Name', 'MISSING') if cached_result else 'None'}")
76+
77+
# Fall back to original function and cache the complete result
78+
fallback_result = _original_get_term_info(short_form, preview)
79+
if is_valid_term_info_result(fallback_result):
80+
print(f"DEBUG: Fallback successful, caching complete result for {short_form}")
81+
cache.set(cache_key, fallback_result)
82+
return fallback_result
83+
else:
84+
print(f"DEBUG: Using valid cached result for {short_form}")
85+
return cached_result
6286

6387
parsed_object = None
6488
try:
@@ -69,15 +93,53 @@ def get_term_info_cached(short_form: str, preview: bool = False):
6993
parsed_object = cached_term_info_parse_object(results, short_form)
7094

7195
if parsed_object:
72-
# Use cached query result filling
73-
term_info = cached_fill_query_results(parsed_object)
74-
if not term_info:
75-
print("Failed to fill query preview results!")
76-
return parsed_object
77-
78-
# Cache the complete result
79-
cache.set(cache_key, parsed_object)
80-
return parsed_object
96+
# Use cached query result filling (skip if queries would fail)
97+
if parsed_object.get('Queries') and len(parsed_object['Queries']) > 0:
98+
try:
99+
term_info = cached_fill_query_results(parsed_object)
100+
if term_info:
101+
# Validate result before caching
102+
if term_info.get('Id') and term_info.get('Name'):
103+
# Cache the complete result
104+
cache.set(cache_key, term_info)
105+
return term_info
106+
else:
107+
print(f"Query result for {short_form} is incomplete, falling back to original function...")
108+
return _original_get_term_info(short_form, preview)
109+
else:
110+
print("Failed to fill query preview results!")
111+
# Validate result before caching
112+
if parsed_object.get('Id') and parsed_object.get('Name'):
113+
# Cache the complete result
114+
cache.set(cache_key, parsed_object)
115+
return parsed_object
116+
else:
117+
print(f"Parsed object for {short_form} is incomplete, falling back to original function...")
118+
return _original_get_term_info(short_form, preview)
119+
except Exception as e:
120+
print(f"Error filling query results (continuing without query data): {e}")
121+
# Validate result before caching
122+
if is_valid_term_info_result(parsed_object):
123+
cache.set(cache_key, parsed_object)
124+
return parsed_object
125+
else:
126+
print(f"DEBUG: Exception case - parsed object incomplete for {short_form}, falling back to original function")
127+
fallback_result = _original_get_term_info(short_form, preview)
128+
if is_valid_term_info_result(fallback_result):
129+
cache.set(cache_key, fallback_result)
130+
return fallback_result
131+
else:
132+
# No queries to fill, validate result before caching
133+
if parsed_object.get('Id') and parsed_object.get('Name'):
134+
# Cache and return parsed object directly
135+
cache.set(cache_key, parsed_object)
136+
return parsed_object
137+
else:
138+
print(f"DEBUG: No queries case - parsed object incomplete for {short_form}, falling back to original function...")
139+
fallback_result = _original_get_term_info(short_form, preview)
140+
if is_valid_term_info_result(fallback_result):
141+
cache.set(cache_key, fallback_result)
142+
return fallback_result
81143
else:
82144
print(f"No valid term info found for ID '{short_form}'")
83145
return None

src/vfbquery/solr_result_cache.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,17 +552,39 @@ def wrapper(*args, **kwargs):
552552
# Try cache first
553553
cached_result = cache.get_cached_result(query_type, term_id, **kwargs)
554554
if cached_result is not None:
555-
return cached_result
555+
# Validate that cached result has essential fields for term_info
556+
if query_type == 'term_info':
557+
if (cached_result and isinstance(cached_result, dict) and
558+
cached_result.get('Id') and cached_result.get('Name')):
559+
logger.debug(f"Using valid cached result for {term_id}")
560+
return cached_result
561+
else:
562+
logger.warning(f"Cached result incomplete for {term_id}, re-executing function")
563+
# Don't return the incomplete cached result, continue to execute function
564+
else:
565+
return cached_result
556566

557567
# Execute function and cache result
558568
result = func(*args, **kwargs)
559569

560570
# Cache the result asynchronously to avoid blocking
561571
if result:
562-
try:
563-
cache.cache_result(query_type, term_id, result, **kwargs)
564-
except Exception as e:
565-
logger.debug(f"Failed to cache result: {e}")
572+
# Validate result before caching for term_info
573+
if query_type == 'term_info':
574+
if (result and isinstance(result, dict) and
575+
result.get('Id') and result.get('Name')):
576+
try:
577+
cache.cache_result(query_type, term_id, result, **kwargs)
578+
logger.debug(f"Cached complete result for {term_id}")
579+
except Exception as e:
580+
logger.debug(f"Failed to cache result: {e}")
581+
else:
582+
logger.warning(f"Not caching incomplete result for {term_id}")
583+
else:
584+
try:
585+
cache.cache_result(query_type, term_id, result, **kwargs)
586+
except Exception as e:
587+
logger.debug(f"Failed to cache result: {e}")
566588

567589
return result
568590

src/vfbquery/vfb_queries.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,11 +854,21 @@ def get_term_info(short_form: str, preview: bool = False):
854854
# Check if any results were returned
855855
parsed_object = term_info_parse_object(results, short_form)
856856
if parsed_object:
857-
term_info = fill_query_results(parsed_object)
858-
if not term_info:
859-
print("Failed to fill query preview results!")
857+
# Only try to fill query results if there are queries to fill
858+
if parsed_object.get('Queries') and len(parsed_object['Queries']) > 0:
859+
try:
860+
term_info = fill_query_results(parsed_object)
861+
if term_info:
862+
return term_info
863+
else:
864+
print("Failed to fill query preview results!")
865+
return parsed_object
866+
except Exception as e:
867+
print(f"Error filling query results (continuing without query data): {e}")
868+
return parsed_object
869+
else:
870+
# No queries to fill, return parsed object directly
860871
return parsed_object
861-
return parsed_object
862872
else:
863873
print(f"No valid term info found for ID '{short_form}'")
864874
return None

0 commit comments

Comments
 (0)