@@ -666,21 +666,34 @@ def wrapper(*args, **kwargs):
666666 # Cache the result asynchronously to avoid blocking
667667 # Handle DataFrame, dict, and other result types properly
668668 result_is_valid = False
669+ result_is_error = False # Track if result is an error that should clear cache
670+
669671 if result is not None :
670672 if hasattr (result , 'empty' ): # DataFrame
671673 result_is_valid = not result .empty
672674 elif isinstance (result , dict ):
673675 # For dict results, check if it's not an error result (count != -1)
674676 # Error results should not be cached
675677 if 'count' in result :
676- result_is_valid = result .get ('count' , - 1 ) >= 0 # Don't cache errors (count=-1)
678+ count_value = result .get ('count' , - 1 )
679+ result_is_valid = count_value >= 0 # Don't cache errors (count=-1)
680+ result_is_error = count_value < 0 # Mark as error if count is negative
677681 else :
678682 result_is_valid = bool (result ) # For dicts without count field
679683 elif isinstance (result , (list , str )):
680684 result_is_valid = len (result ) > 0
681685 else :
682686 result_is_valid = True
683687
688+ # If result is an error, actively clear any existing cache entry
689+ # This ensures that transient failures don't get stuck in cache
690+ if result_is_error :
691+ logger .warning (f"Query returned error result for { query_type } ({ term_id } ), clearing cache entry" )
692+ try :
693+ cache .clear_cache_entry (query_type , cache_term_id )
694+ except Exception as e :
695+ logger .debug (f"Failed to clear cache entry: { e } " )
696+
684697 if result_is_valid :
685698 # Validate result before caching for term_info
686699 if query_type == 'term_info' :
0 commit comments