66"""
77
88from typing import Dict , Any , Optional
9- from .cache_enhancements import cache_result , get_cache
9+ from .solr_result_cache import with_solr_cache
1010
1111
1212def is_valid_term_info_result (result ):
@@ -45,40 +45,20 @@ def is_valid_term_info_result(result):
4545from .vfb_queries import (
4646 get_term_info as _original_get_term_info ,
4747 get_instances as _original_get_instances ,
48- vfb_solr ,
49- term_info_parse_object as _original_term_info_parse_object ,
50- fill_query_results as _original_fill_query_results
48+ vfb_solr
5149)
5250
53- @cache_result ("solr_search" , "solr_cache_enabled " )
51+ @with_solr_cache ("solr_search" )
5452def cached_solr_search (query : str ):
5553 """Cached version of SOLR search."""
5654 return vfb_solr .search (query )
5755
58- @cache_result ("term_info_parse" , "term_info_cache_enabled" )
59- def cached_term_info_parse_object (results , short_form : str ):
60- """Cached version of term_info_parse_object."""
61- return _original_term_info_parse_object (results , short_form )
62-
63- @cache_result ("query_results" , "query_result_cache_enabled" )
64- def cached_fill_query_results (term_info : Dict [str , Any ]):
65- """Cached version of fill_query_results."""
66- return _original_fill_query_results (term_info )
67-
68- @cache_result ("get_instances" , "query_result_cache_enabled" )
69- def cached_get_instances (short_form : str , return_dataframe = True , limit : int = - 1 ):
70- """Cached version of get_instances."""
71- return _original_get_instances (short_form , return_dataframe , limit )
72-
56+ @with_solr_cache ("term_info" )
7357def get_term_info_cached (short_form : str , preview : bool = False ):
7458 """
75- Enhanced get_term_info with multi-layer caching.
59+ Enhanced get_term_info with SOLR caching.
7660
77- This version uses caching at multiple levels:
78- 1. Final result caching (entire term_info response)
79- 2. SOLR query result caching
80- 3. Term info parsing caching
81- 4. Query result caching
61+ This version caches complete term_info responses in SOLR for fast retrieval.
8262
8363 Args:
8464 short_form: Term short form (e.g., 'FBbt_00003748')
@@ -87,104 +67,14 @@ def get_term_info_cached(short_form: str, preview: bool = False):
8767 Returns:
8868 Term info dictionary or None if not found
8969 """
90- cache = get_cache ()
91-
92- # Check for complete result in cache first
93- cache_key = cache ._generate_cache_key ("term_info_complete" , short_form , preview )
94- cached_result = cache .get (cache_key )
95- print (f"DEBUG: Cache lookup for { short_form } : { 'HIT' if cached_result is not None else 'MISS' } " )
96- if cached_result is not None :
97- # Validate that cached result has essential fields
98- if not is_valid_term_info_result (cached_result ):
99- print (f"DEBUG: Cached result incomplete for { short_form } , falling back to original function" )
100- print (f"DEBUG: cached_result keys: { list (cached_result .keys ()) if cached_result else 'None' } " )
101- print (f"DEBUG: cached_result Id: { cached_result .get ('Id' , 'MISSING' ) if cached_result else 'None' } " )
102- print (f"DEBUG: cached_result Name: { cached_result .get ('Name' , 'MISSING' ) if cached_result else 'None' } " )
103-
104- # Fall back to original function and cache the complete result
105- fallback_result = _original_get_term_info (short_form , preview )
106- if is_valid_term_info_result (fallback_result ):
107- print (f"DEBUG: Fallback successful, caching complete result for { short_form } " )
108- cache .set (cache_key , fallback_result )
109- return fallback_result
110- else :
111- print (f"DEBUG: Using valid cached result for { short_form } " )
112- return cached_result
113-
114- parsed_object = None
115- try :
116- # Use cached SOLR search
117- results = cached_solr_search ('id:' + short_form )
118-
119- # Use cached term info parsing
120- parsed_object = cached_term_info_parse_object (results , short_form )
121-
122- if parsed_object :
123- # Use cached query result filling (skip if queries would fail)
124- if parsed_object .get ('Queries' ) and len (parsed_object ['Queries' ]) > 0 :
125- try :
126- term_info = cached_fill_query_results (parsed_object )
127- if term_info :
128- # Validate result before caching
129- if term_info .get ('Id' ) and term_info .get ('Name' ):
130- # Cache the complete result
131- cache .set (cache_key , term_info )
132- return term_info
133- else :
134- print (f"Query result for { short_form } is incomplete, falling back to original function..." )
135- return _original_get_term_info (short_form , preview )
136- else :
137- print ("Failed to fill query preview results!" )
138- # Validate result before caching
139- if parsed_object .get ('Id' ) and parsed_object .get ('Name' ):
140- # Cache the complete result
141- cache .set (cache_key , parsed_object )
142- return parsed_object
143- else :
144- print (f"Parsed object for { short_form } is incomplete, falling back to original function..." )
145- return _original_get_term_info (short_form , preview )
146- except Exception as e :
147- print (f"Error filling query results (continuing without query data): { e } " )
148- # Validate result before caching
149- if is_valid_term_info_result (parsed_object ):
150- cache .set (cache_key , parsed_object )
151- return parsed_object
152- else :
153- print (f"DEBUG: Exception case - parsed object incomplete for { short_form } , falling back to original function" )
154- fallback_result = _original_get_term_info (short_form , preview )
155- if is_valid_term_info_result (fallback_result ):
156- cache .set (cache_key , fallback_result )
157- return fallback_result
158- else :
159- # No queries to fill, validate result before caching
160- if parsed_object .get ('Id' ) and parsed_object .get ('Name' ):
161- # Cache and return parsed object directly
162- cache .set (cache_key , parsed_object )
163- return parsed_object
164- else :
165- print (f"DEBUG: No queries case - parsed object incomplete for { short_form } , falling back to original function..." )
166- fallback_result = _original_get_term_info (short_form , preview )
167- if is_valid_term_info_result (fallback_result ):
168- cache .set (cache_key , fallback_result )
169- return fallback_result
170- else :
171- print (f"No valid term info found for ID '{ short_form } '" )
172- return None
173-
174- except Exception as e :
175- print (f"Error in cached get_term_info: { type (e ).__name__ } : { e } " )
176- # Fall back to original function if caching fails
177- return _original_get_term_info (short_form , preview )
70+ return _original_get_term_info (short_form , preview )
17871
72+ @with_solr_cache ("instances" )
17973def get_instances_cached (short_form : str , return_dataframe = True , limit : int = - 1 ):
18074 """
181- Enhanced get_instances with caching.
75+ Enhanced get_instances with SOLR caching.
18276
183- This cached version can provide dramatic speedup for repeated queries,
184- especially useful for:
185- - UI applications with repeated browsing
186- - Data analysis workflows
187- - Testing and development
77+ This cached version provides dramatic speedup for repeated queries.
18878
18979 Args:
19080 short_form: Class short form
@@ -194,7 +84,7 @@ def get_instances_cached(short_form: str, return_dataframe=True, limit: int = -1
19484 Returns:
19585 Instances data (DataFrame or formatted dict based on return_dataframe)
19686 """
197- return cached_get_instances (short_form , return_dataframe , limit )
87+ return _original_get_instances (short_form , return_dataframe , limit )
19888
19989# Convenience function to replace original functions
20090def patch_vfbquery_with_caching ():
0 commit comments