77
88from typing import Dict , Any , Optional
99from .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' )
1018from .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
0 commit comments